recursive displaying tree structure

From: Date: Thu, 05 Jun 2003 12:17:44 +0000
Subject: recursive displaying tree structure
Groups: php.general 
Request: Send a blank email to php-general+get-150346@lists.php.net to get a copy of this message
Hello, If anyone have expamples of displaying tree structure. Say I have array $result[x][y] with folloowing data X Y["ID"] Y["OWNER"] 0 1 0 1 2 1 2 3 2 3 4 2 4 5 3 5 6 2 6 7 2 So, I want it displayed like ID1 ID2 ID3 ID5 ID4 ID6 ID7 ======================== Please, help I tryed to do it with recursing but failed. There is a test function which shows that php doesnt support recursing well:- function parse($id){ global $result; $idarr=getChildren($id); // thsi function return indexes of child nodes of $id or empt array. for ($i=0;$i<count($idarr);$i++){ echo $result[$idarr[$i]]["id"]; $id=$result[$idarr[$i]]["id"]; parse($id); } } Actually it should print me only nodes which has childs, like ID1, ID2 and ID3 but it shows everything. getChildren- function getChildren($id){ global $result; $children = array(); for ($i=0;$i<count($result);$i++){ if ($id==$result[$i]["owner"]){ while ($i<count($result)){ if ($id==$result[$i]["owner"]){ array_push($children,$i); } else{ break; } $i++; } break; } } return $children; }

« previous php.general (#150346) next »