在PHP中构建递归菜单树
我试图构建一个递归菜单树,它输出/构建一个数组,而不是标记。我已经找到了标记方法,但无法将其构建到PHP数组中 我使用ProcessWireAPI进行一些子项检查,但除此之外,它是常规的PHP在PHP中构建递归菜单树,php,json,Php,Json,我试图构建一个递归菜单树,它输出/构建一个数组,而不是标记。我已经找到了标记方法,但无法将其构建到PHP数组中 我使用ProcessWireAPI进行一些子项检查,但除此之外,它是常规的PHP function menuTree(Page $parent, $enter, $exit = null) { foreach ($parent->children() as $child) { call_user_func($enter, $child);
function menuTree(Page $parent, $enter, $exit = null) {
foreach ($parent->children() as $child) {
call_user_func($enter, $child);
if ($child->numChildren > 0) {
menuTree($child, $enter, $exit);
}
if ($exit) {
call_user_func($exit, $child);
}
}
}
menuTree($pages->get(1131), function(Page $page) {
echo '<li><a href="' . $page->url . '">' . $page->title . '</a>';
if ($page->numChildren > 0) {
echo '<ul>';
}}, function(Page $page) {
echo '</li>';
if ($page->numChildren > 0) {
echo '</ul>';
}
});
你想把它变成一个数组的尝试在哪里?你到底被困在哪里了?还有,在转换成HTML的对象中已经存在的现有页面->子结构有什么问题?看来这在很大程度上会起作用job@ADysonpage->children()不是递归的,我不确定将我的数组()放在哪里。首先,将填充数组的调用放在(某些)echo语句的位置可能会有一些意义。因为这正是你想要取代的。如果你想不出来,先把你的逻辑写在纸上。
menu => array(
main => array(
1023 => array(
id => 1023
title => 'Menu item 1',
children => null
),
1024 => array(
id => 1024
title => 'Menu item 1',
children => array(
3000 => array(
id => 3000
title => 'Child menu item 1',
children => null
),
3001 => array(
id => 3001
title => 'Child menu item 2',
children => null
)
)
),
1024 => array(
id => 1024
title => 'Menu item 1',
children => array(
3000 => array(
id => 3000
title => 'Child menu item 1',
children => array(
4001 => array(
id => 4001
title => 'Child menu item 2',
children => null
),
4002 => array(
id => 4002
title => 'Child menu item 2',
children => null
)
)
),
3001 => array(
id => 3001
title => 'Child menu item 2',
children => null
)
)
)
)
)