数组数据结构:
id name parent_id children
现在我有一个根数组和一组子数组,我想要构建一棵树形结构,以下是我所拥有的:
更新:
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);
}
}
这将返回相同的根节点,而不是添加的子节点。请问有人能帮忙解决吗?非常感谢。
更新:print_r($val) 输出:
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
)
.....
foreach($arr as $key=>$val)
然后unset($arr[$key])
。 - k102$children
数组上。我猜它是空的。 - k102