如何在多维数组中向上传播当前菜单项标志?
我有一个通过多维数组构建的菜单。当前项目是通过将其 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
I have a menu being built via a multi-dimensional array. A current item is set by matching its url attribute with the current request. I want this value to bubble up to its parents, but I can't for the life of me get it working - I'm sure I've been close, but it's proving a bit tricky. Here's the array:
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
Essentially, I'm looking for a function that will iterate through every item until it finds the [current] flag, then propagate that value back up to its parents. There are no set limits on depth in the menu.
For the example above, it would result in:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就像
未经测试一样,因为您发布的数据不适合测试(哦,亲爱的,为什么他们不断发布这些 var_dumps?)
like
not tested, because you posted data unsuitable for testing (oh dear, why why do they keep posting those var_dumps?)
这是修改后的工作版本:
感谢您的帮助;在过去的几天里,我一直非常接近,只需要看到它的写法略有不同即可完成它。
Here's the modified, working version:
Thanks for the help; I'd been very close repeatedly over the past few days, just needed to see it written slightly differently to finish it off.