我在 WP 中有一个充满导航菜单项的平面数组。每个项目都知道它的父母,但他们似乎都不知道他们是否有孩子。
Array
(
[0] => stdClass Object
(
[ID] => 22
[menu_item_parent] => 0
)
[1] => stdClass Object
(
[ID] => 108
[menu_item_parent] => 22
)
[2] => stdClass Object
(
[ID] => 117
[menu_item_parent] => 108
)
[3] => stdClass Object
(
[ID] => 118
[menu_item_parent] => 108
)
[4] => stdClass Object
(
[ID] => 106
[menu_item_parent] => 22
)
[5] => stdClass Object
(
[ID] => 119
[menu_item_parent] => 106
)
[6] => stdClass Object
(
[ID] => 120
[menu_item_parent] => 106
)
[7] => stdClass Object
(
[ID] => 23
[menu_item_parent] => 0
)
)
我已经尝试用几种不同的方式用条件逻辑来处理它(每次都硬编码值并不理想),但我一直把自己编码成一团糟。如何遍历数组以产生这样的结果?
<ul>
<li>22
<ul class="child">
<li>108
<ul class="grandchild">
<li>117</li>
<li>118</li>
</ul>
</li>
<li>106
<ul class="grandchild">
<li>119</li>
<li>120</li>
</ul>
</li>
</ul>
</li>
<li>23</li>
</ul>