fork(1) download
  1. <?php
  2.  
  3. function add_menu_item_path(&$menu_data, $path = '') {
  4.  
  5. foreach ($menu_data as &$menu_item) {
  6. $path = $path . '/' . $menu_item['key'];
  7. $menu_item['path'] = $path;
  8. if (!empty($menu_item['children'])) {
  9. add_menu_item_path($menu_item['children'], $path);
  10. }
  11. }
  12. }
  13.  
  14.  
  15. $menu_items = array(
  16. 'key' => 'aaa',
  17. 'children' => array(
  18. 'key' => 'www'
  19. ),
  20. 'key' => 'xxx'
  21. ),
  22. )
  23. ),
  24. 'key' => 'bbb',
  25. 'children' => array(
  26. 'key' => 'yyy'
  27. ),
  28. 'key' => 'zzz'
  29. ),
  30. )
  31. ),
  32. );
  33.  
  34.  
  35. add_menu_item_path($menu_items);
  36.  
  37.  
  38. print_r($menu_items);
  39.  
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
Array
(
    [0] => Array
        (
            [key] => aaa
            [children] => Array
                (
                    [0] => Array
                        (
                            [key] => www
                            [path] => /aaa/www
                        )

                    [1] => Array
                        (
                            [key] => xxx
                            [path] => /aaa/www/xxx
                        )

                )

            [path] => /aaa
        )

    [1] => Array
        (
            [key] => bbb
            [children] => Array
                (
                    [0] => Array
                        (
                            [key] => yyy
                            [path] => /aaa/bbb/yyy
                        )

                    [1] => Array
                        (
                            [key] => zzz
                            [path] => /aaa/bbb/yyy/zzz
                        )

                )

            [path] => /aaa/bbb
        )

)