我有一个递归例子,我想知道的几件事。这是例子:PHP - 递归例子解释
private function menuBuilder($menuItems, $parentId = 0)
{
$res = [];
foreach ($menuItems as $index => $item) {
$itemParentId = $item->meta->_menu_item_menu_item_parent;
if ($itemParentId == $parentId) {
$children = $this->menuBuilder($menuItems, $item->ID);
if ($children) {
$item['submenu'] = $children;
}
$res[$item->ID] = $item;
unset($menuItems[$index]);
}
}
return $res;
}
这该函数返回$res
:
{
"112": {
"ID": 112,
"submenu": {
"135": {
"ID": 135,
"title": "",
"slug": "135",
"url": "http://hivnorge.app/?p=135",
"status": "publish",
"main_category": "Hovedmeny"
},
"137": {
"ID": 137,
"submenu": {
"138": {
"ID": 138,
"title": "",
"slug": "138",
"url": "http://hivnorge.app/?p=138",
"status": "publish",
"main_category": "Hovedmeny"
}
},
"title": "",
"slug": "137",
"url": "http://hivnorge.app/?p=137",
"status": "publish",
"main_category": "Hovedmeny"
}
},
"title": "Hjem",
"slug": "hjem",
"url": "http://hivnorge.app/?p=112",
"status": "publish",
"main_category": "Hovedmeny"
},
"136": {
"ID": 136,
"title": "",
"slug": "136",
"url": "http://hivnorge.app/?p=136",
"status": "publish",
"main_category": "Hovedmeny"
},
"139": {
"ID": 139,
"title": "",
"slug": "139",
"url": "http://hivnorge.app/?p=139",
"status": "publish",
"main_category": "Hovedmeny"
}
}
在这里,我传递一个平面数组$menuItems
与每个都有一组parent_id
场的菜单项,以及items
已将parent_id
设置为0
位于树状结构的根部。我不知道在这个递归方法,因为我从来没有做一个,是,当具有parent_id
设置为0
别的其他东西的第一个元素,通过foreach循环,确实值在默认参数$parentId = 0
变化超过设定在接下来的item
中foreach
循环? 所以,为了说明我的困惑,我会尝试用下面的例子来解释它。
因此,例如,如果在$menuItems
第一$item
有$itemParentId = 0
,比它通过条件
if ($itemParentId == $parentId)
,并再次调用该函数在这里:
$children = $this->menuBuilder($menuItems, $item->ID);
所以,我路过第一$item
的ID
,例如112
和再次调用该函数,其中$parentId
等于112
。然后当函数再次被调用时,我们进入foreach循环并且从第一个开始$item
开始,并且由于$parentId
现在是112
而且$item parent_id
是0
我们不通过条件if ($itemParentId == $parentId)
并且$children
不返回任何东西这一轮。所以,我们刚刚结束了
$res[$item->ID] = $item
但是,那么我不知道,当第二item
自带例如会发生什么,不具备parent_id
设置为0,但对112
,这是前面的item
的ID
。我们如何填补submenu
阵列时,它不会通过的条件
if ($itemParentId == $parentId)
如果parentId
留0
我不知道,如果我的流程的解释是正确的,在所有的,但是我如何计算的,所以如果有人能够清楚这一点,我将非常感激。 这是平坦$menuItems
数组中的元素:
#attributes: array:23 [
"ID" => 135
"post_author" => 1
"post_date" => "2017-06-23 10:59:47"
"post_date_gmt" => "2017-06-23 08:59:47"
"post_content" => " "
"post_title" => ""
"post_excerpt" => ""
"post_status" => "publish"
"comment_status" => "closed"
"ping_status" => "closed"
"post_password" => ""
"post_name" => "135"
"to_ping" => ""
"pinged" => ""
"post_modified" => "2017-06-29 09:37:49"
"post_modified_gmt" => "2017-06-29 07:37:49"
"post_content_filtered" => ""
"post_parent" => 0
"guid" => "http://hivnorge.app/?p=135"
"menu_order" => 2
"post_type" => "nav_menu_item"
"post_mime_type" => ""
"comment_count" => 0
]
"meta" => PostMetaCollection {#243
#attributes: array:4 [
"meta_id" => 326
"post_id" => 135
"meta_key" => "_menu_item_menu_item_parent"
"meta_value" => "112"
]
但是,如果第二个项目的父项ID是除0之外的某个数字,并且parentId参数保持为0,那么第二个项目如何进入块'''if if($ itemParentId == $ parentId)'''这就是我不明白的地方,我会尝试扩展我的问题来说明我对此的困惑 – Leff
'$ itemParentId'来自'$ item'本身 - 它可能有一个与之关联的父项。 '$ parentId'参数是可选的,以防您只想获得与特定父级关联的'$ menuItems'。我假设'$ item'的默认父值是0(顶级菜单项)。 –