我有树解决方案,但我想将其转换为数组......
function generatePageTree($datas , $parent = 0)
$tree = '<ul>';
for($i=0, $ni=count($datas); $i < $ni; $i++){
if($datas[$i]['parent_id'] == $parent){
$tree .= '<li>';
$tree .= $datas[$i]['ledger_account_name'];
$tree .= $this->generatePageTree($datas, $datas[$i]['ledger_account_id']);
$tree .= '</li>';
}
}
$tree .= '</ul>';
return $tree;
}
我希望这个树形结构以数组的形式..任何一个已经完成的请帮助.... 数组格式............
array(
id=>100, parentid=>0, name=>'a', children=>array(
id=>101, parentid=>100, name=>'a', children=>array(
id=>102, parentid=>101, name=>'a',
id=>103, parentid=>101, name=>'a',
)
)
)
答案 0 :(得分:2)
function buildTree(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if ($element['parent_id'] == $parentId) {
$children = $this->buildTree($elements, $element['ledger_account_id']);
if ($children) {
$element['children'] = $children;
}
$branch[$element['ledger_account_id']] = $element;
}
}
return $branch;
}