我正在检索一个数组,并使用函数来构造该数组,使其看起来像树数组。如何使用列表标签(ul,li)制作树形菜单
function buildTree(array &$elements, $parentId = 0, $elkey = null) {
$branch = array();
foreach ($elements as $key=>$element) {
if ($element[$elkey]['parent_id'] == $parentId) {
$children = buildTree($elements, $element[$elkey]['id'], $elkey);
if ($children) {
$element['children'] = $children;
}
$branch[$element[$elkey]['id']] = $element;
unset($elements[$key]);
}
}
return $branch;
}
$menuTree = buildTree($systemPageCategories, 0, 'SystemPageCategory');
debug($menuTree);
打印=>
....
...
(int) 5 => array(
'SystemPageCategory' => array(
'id' => '5',
'parent_id' => '0',
'title' => 'Administration',
'url' => '',
'sort' => '5',
'published' => '1'
)
),
(int) 6 => array(
'SystemPageCategory' => array(
'id' => '6',
'parent_id' => '0',
'title' => 'Reports',
'url' => '',
'sort' => '6',
'published' => '1'
),
'children' => array(
(int) 7 => array(
'SystemPageCategory' => array(
'id' => '7',
'parent_id' => '6',
'title' => 'Revenue',
'url' => '',
'sort' => '1',
'published' => '1'
),
'children' => array(
(int) 13 => array(
'SystemPageCategory' => array(
'id' => '13',
'parent_id' => '7',
'title' => 'OBE Finance - Revenue (Current Monthly)',
'url' => '',
'sort' => '1',
'published' => '1'
)
),
(int) 14 => array(
'SystemPageCategory' => array(
'id' => '14',
'parent_id' => '7',
'title' => 'OBE Finance - Revenue (Snapshot Monthly)',
'url' => '',
'sort' => '2',
'published' => '1'
)
),
....
...
...