I have an SQL connection which outputs an array of objects and it is sorted like so
(
[0] => listItem Object
(
[name] => TV
[parentId] => 0
[id] => 1
[childs] => Array
(
[0] => listItem Object
(
[name] => mini tv
[parentId] => 1
[id] => 29
[childs] => Array
(
[0] => listItem Object
(
[name] => tiny TV
[parentId] => 29
[id] => 548
)
)
)
)
)
[1] => listItem Object
(
[name] => RADIO
[parentId] => 0
[id] => 2
[childs] => Array
(
[0] => listItem Object
(
[name] => mini radio
[parentId] => 2
[id] => 30
)
[1] => listItem Object
(
[name] => Tiny LCD
[parentId] => 2
[id] => 551
)
)
)
[2] => listItem Object
(
[name] => Stereo
[parentId] => 0
[id] => 550
)
[3] => listItem Object
(
[name] => Joe the Plumber
[parentId] => 0
[id] => 568
[childs] => Array
(
[0] => listItem Object
(
[name] => Joe the Plumber
[parentId] => 568
[id] => 569
[childs] => Array
(
[0] => listItem Object
(
[name] => Joe the Plumber
[parentId] => 569
[id] => 570
[childs] => Array
(
[0] => listItem Object
(
[name] => Joe the Plumber
[parentId] => 570
[id] => 571
)
)
)
)
)
)
)
)
I am sorting it this way since I want to output it as a with the proper hierarchy.
I have this function
function buildHTML($tree){
echo 'building tree';
foreach($tree as $item){
$topItem = makeHtmlListItem($item);
$pos = strpos($topItem, '</li>');
$newItem = substr($topItem,0 , $pos);
echo $newItem;
foreach($item->childs as $subItem){
echo '<ul>' . makeHtmlListItem($subItem) . '</ul>' ;
}
echo '</li>';
}
}
But it only gets to the second level. How do I get to the bottom with an arbitrary depth? I managed to do it with recursion but now I want to do it without recursion and I am going nuts.