如何在多维数组中向上传播当前菜单项标志?

发布于 2024-08-22 17:54:33 字数 2508 浏览 3 评论 0原文

我有一个通过多维数组构建的菜单。当前项目是通过将其 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娜些时光,永不杰束 2024-08-29 17:54:33

就像

 function set_current($menu) {
    if(!is_array($menu)) return 0;
    if(isset($menu['current'])) return 1;
    foreach($menu as $sub) {
        if(set_current($sub))
            return $menu['current'] = 1;
    }
    return 0;
}

未经测试一样,因为您发布的数据不适合测试(哦,亲爱的,为什么他们不断发布这些 var_dumps?)

like

 function set_current($menu) {
    if(!is_array($menu)) return 0;
    if(isset($menu['current'])) return 1;
    foreach($menu as $sub) {
        if(set_current($sub))
            return $menu['current'] = 1;
    }
    return 0;
}

not tested, because you posted data unsuitable for testing (oh dear, why why do they keep posting those var_dumps?)

凉世弥音 2024-08-29 17:54:33

这是修改后的工作版本:

function set_current(&$menu) {
    if(!is_array($menu)) return false;
    if(isset($menu['current'])) return true;
    if(isset($menu['children'])){
        foreach($menu['children'] as $key => $value) {
            if(set_current($menu['children'][$key])){
                $menu['current'] = true;
                return true;
            }
        }
    }
    return false;
}

感谢您的帮助;在过去的几天里,我一直非常接近,只需要看到它的写法略有不同即可完成它。

Here's the modified, working version:

function set_current(&$menu) {
    if(!is_array($menu)) return false;
    if(isset($menu['current'])) return true;
    if(isset($menu['children'])){
        foreach($menu['children'] as $key => $value) {
            if(set_current($menu['children'][$key])){
                $menu['current'] = true;
                return true;
            }
        }
    }
    return false;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文