i’m referring this address for function olLiTree
PHP function that creates a nested ul li?
i have this array
$tree =
array(“A”=>array(“B”=>array(“C”=>”C”,”D”=>”D”),”E”=>array(“F”=>”F”,”G”=>”G”)));
but not able to use this function
function olLiTree($tree)
{
echo '<ul>';
foreach($tree as $item) {
if (is_array($item)) {
olLiTree($item);
} else {
echo '<li>', $item, '</li>';
}
}
echo '</ul>';
}
to generate
<ul>
<li>A</li>
<li>B
<ul>
<li>C</li>
<li>D</li>
</ul>
</li>
<li>E
<ul>
<li>F
</li>
</ul>
</li>
<ul>
<li>G</li>
</ul>
</ul>
can anybody help me to fix this?
thanks..
Always
returnthe final output, don’techoit directly. You’ll thank yourself one day when you find a situation where you don’t want toechoit immediately. 🙂You might want to change the array structure to this, as it’s less redundant (works with above code):