Skip to content Skip to sidebar Skip to footer

Multidimensional Array To Html Unordered List

I have the following array which I want to transform into an unordered list (HTML). Array ( [0] => Array ( [url] => / [id] => 53a8717fe

Solution 1:

Try a function like this:

functionToUl($input){
   echo"<ul>";

   $oldvalue = null;
   foreach($inputas$value){
     if($oldvalue != null && !is_array($value))
        echo"</li>";
     if(is_array($value)){
        ToUl($value);
     }elseecho"<li>" + $value;
      $oldvalue = $value;
    }

    if($oldvalue != null)
      echo"</li>";

   echo"</ul>";
}

[Edit]

I'll leave the function which creates a li for every array, which is simpler, in case any reader needs it:

functionToUl($input){
   echo"<ul>";

   foreach($inputas$value)
     if(is_array($value)){
        echo"<li>";
        ToUl($value);
        echo"</li>";
     }elseecho"<li>" + $value + "</li>";

   echo"</ul>";
}

Post a Comment for "Multidimensional Array To Html Unordered List"