Given this array:
$menu_items = array(
array(
'key' => 'aaa',
'children' => array(
array(
'key' => 'www'
),
array(
'key' => 'xxx'
),
)
),
array(
'key' => 'bbb',
'children' => array(
array(
'key' => 'yyy'
),
array(
'key' => 'zzz'
),
)
),
);
I would like to add path to each node in the array like this:
$menu_items = array(
array(
'key' => 'aaa',
'path' => 'aaa',
'children' => array(
array(
'key' => 'www',
'path' => 'aaa/www'
),
array(
'key' => 'xxx',
'path' => 'aaa/xxx'
),
)
),
array(
'key' => 'bbb',
'path' => 'bbb',
'children' => array(
array(
'key' => 'yyy',
'path' => 'bbb/yyy',
),
array(
'key' => 'zzz',
'path' => 'bbb/zzz',
),
)
),
);
This menu item array only has 2 levels but it can have more levels.
What I have tried:
function add_menu_item_path(&$menu_data, $path = '') {
foreach ($menu_data as &$menu_item) {
$path = $path . '/' . $menu_item['key'];
$menu_item['path'] = $path;
if (!empty($menu_item['children'])) {
add_menu_item_path($menu_item['children'], $path);
}
}
}
This does not work as expected, it can be viewed here: http://ideone.com/sHdhss