array data structure:
id name parent_id children
now I have a root array, and a set of array of children, I want to build a tree structure, and this is what I have:
Updated::
function buildTree($root,$children)
{
foreach($children as $key=>$val){
print_r($val);
$val['children']=array();
if($val['parent_id']==$root['id']){
$root['children'][]=$val;
//remove it so we don't need to go through again
unset($children[$key]);
}
}
if(count($root['children'])==0)return;
foreach($root['children'] as $child){
$this->buildTree($child,$children);
}
}
this returns the same root,,not children added could anybody helps me with this. thanks a lot.
update: print_r($val) print out:
Array
(
[id] => 3
[name] => parent directory2
[type] => d
[creat_time] => 2011-07-08 06:38:36
[parent_id] => 1
[user_id] => 1
)
Array
(
[id] => 5
[name] => parent directory3
[type] => d
[creat_time] => 2011-07-08 06:38:36
[parent_id] => 1
[user_id] => 1
)
.....