Php Nested Array Into Html List
Trying to get to grips with PHP, but I have absolutely no idea how to do this. I want to take this array: $things = array('vehicle' => array('car' => array('hatchback', 'salo
Solution 1:
What you are looking for is called Recursion. Below is a recursive function that calls itself if the value of the array key is also an array.
functionprintArrayList($array)
{
echo"<ul>";
foreach($arrayas$k => $v) {
if (is_array($v)) {
echo"<li>" . $k . "</li>";
printArrayList($v);
continue;
}
echo"<li>" . $v . "</li>";
}
echo"</ul>";
}
Solution 2:
Try something like:
<?phpfunctionToUl($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>";
}
?>
Code source: Multidimensional array to HTML unordered list
Post a Comment for "Php Nested Array Into Html List"