1

我有一个通过多维数组构建的菜单。通过将其 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
4

2 回答 2

3

喜欢

 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?)

于 2010-02-19T13:25:09.450 回答
1

这是修改后的工作版本:

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;
}

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

于 2010-02-19T15:07:30.330 回答