views:

30

answers:

2

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
+1  A: 

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

stereofrog
A: 

Here's the modified, working version of stereofrog's answer:

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.

Throlkim