I have a Multidimensional Associative Array containing a parent page with a position number related to each subpage. I am trying to save time by dynamically running through each array to write the same line of code for each subpage.
$page = array(
"content" => array(0, 3, 1, 2),
"users" => array(17, 5, 16),
"settings" => array(5, 15, 10)
);
foreach ($page as $parent => $ary){
foreach ($ary as $position){
$return = $submenu[$parent][$position];
}
}
I need it to output the following:
$submenu['content'][0];
$submenu['content'][3];
$submenu['content'][1];
$submenu['content'][2];
$submenu['users'][17];
$submenu['users'][5];
$submenu['users'][16];
$submenu['settings'][5];
$submenu['settings'][15];
$submenu['settings'][10];
Everything I have found regarding multidimentional arrays involves a nesting foreach loops but that doesn’t get the proper result. I am fairly new to PHP but from what I can tell, it appears that when nesting foreach loops the initial $parent key from the first loop becomes inaccessible to the second loop.
One thought I had was to create one long combined array, but it just returns [0] => array() [1] => array()
I can’t seem to figure it out. Any help is appreciated, thank you
I tried with something like this:
foreach ($page as $parent => $ary){
$bigarray = array($parent => $ary)
foreach ($bigarray as $parent => $position){
$return = $submenu[$parent][$position];
}
}
CBR600grl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1