我有一个通过多维数组构建的菜单。通过将其 url 属性与当前请求匹配来设置当前项目。我希望这个值能传给它的父母,但我一辈子都无法让它发挥作用——我确信我已经很接近了,但事实证明这有点棘手。这是数组:
Array
(
[children] => Array
(
[Home] => Array
(
[url] => /
)
[The Challenge] => Array
(
[url] => /challenge
)
[The Finish] => Array
(
[url] => /finish
)
[Latest News] => Array
(
[url] => /latest_news
)
[Participants] => Array
(
[url] => /participants
[children] => Array
(
[Some guy] => Array
(
[url] => /participants/some_guy
[children] => Array
(
[Hats] => Array
(
[url] => /participants/some_guy/hats
[current] => 1
本质上,我正在寻找一个函数,它将遍历每个项目,直到找到 [current] 标志,然后将该值传播回其父项。菜单中的深度没有设置限制。
对于上面的示例,它将导致:
Array
(
[children] => Array
(
[Home] => Array
(
[url] => /
)
[The Challenge] => Array
(
[url] => /challenge
)
[The Finish] => Array
(
[url] => /finish
)
[Latest News] => Array
(
[url] => /latest_news
)
[Participants] => Array
(
[url] => /participants
[current] => 1
[children] => Array
(
[Some guy] => Array
(
[url] => /participants/some_guy
[current] => 1
[children] => Array
(
[Hats] => Array
(
[url] => /participants/some_guy/hats
[current] => 1