I have an array, let’s call it $menus, that contains something like this:
Array(
[Main] => Array (
[menu_name] => Main
[title] => Main Menu
)
[Nav] => Array (
[menu_name] => Navigation
[title] => Navigation Menu
)
[Custom] => Array (
[menu_name] => User Custom Menu
[title] => My Menu
)
)
…and so forth.
I need to create a new array that contains only a list of menu names. So if I were going to get, say, just the Nav menu’s name, I’d do
$module_menu_name = $menus [Nav][menu_name];
and now $module_menu_name = “Navigation”, right?
But how do I get the menu_name of each item in an associative array?
Something like:
$menu_names = Array();
foreach($menus as $menu){
$module_menu_names[] => ???['menu_name'];
}
… and this is where I get stuck. What do I put where those question marks are? Is this even the right way to build this array?
Like this?
In your foreach, $menu returns the 2nd-level arrays. To access the ‘menu_name’, just get that value of the 2nd-level array and put it into your $menu_names array.
Note: I changed your $module_menu_names array to $menu_names, since it seemed like that’s what you want to do. If not, just change $menu_names inside the foreach loop to $module_menu_names.