I have this combination of multi-dimensional array but I can’t print the correct value out on the screen,
$parent = array(
array(
'function_name' => 'home',
'screen_name' => 'Home'
),
array(
'function_name' => 'categories',
'screen_name' => 'Categories',
array(
'function_name' => 'post',
'screen_name' => 'Post',
)
),
array(
'function_name' => 'pages',
'screen_name' => 'Pages',
array(
'function_name' => 'add',
'screen_name' => 'Add',
array(
'function_name' => 'manage',
'screen_name' => 'Manage'
)
)
)
);
some arrays are by themselves, some are with sub arrays, and some sub arrays have subsub arrays.
foreach($parent as $index => $item)
{
echo 'parent - '.$item['screen_name'].'<br/>';
foreach($item as $child)
{
echo 'child - '.$child['screen_name'].'<br/>';
foreach($child as $grandchild)
{
echo 'grandchild - '.$grandchild['screen_name'].'<br/>';
}
}
}
error message,
Warning: Invalid argument supplied for foreach() in -> foreach($child as $grandchild)
the result I am looking for is like this,
parent - Home
parent - Categories
child - Post
parent - Pages
child - Add
grandchild - Manage
EDIT:
I tried with this test below,
foreach ($system as $inner_1) {
if (is_array($inner_1))
{
foreach ($inner_1 as $inner_2)
{
if (is_array($inner_2))
{
foreach ($inner_2 as $inner_3)
{
var_dump($inner_3['function_name']);
}
}
}
}
}
but I get a strange result,
string 'p' (length=1) // don't know why I get this.
string 'P' (length=1) // don't know why I get this.
string 'a' (length=1) // don't know why I get this.
string 'A' (length=1) // don't know why I get this.
string 'manage' (length=6) // correct result.
When running
foreach($item as $child)on$parent[1],$childtakes the following values:'home','Home'and an array.What about keeping the arrays purely associative?
This way you can run
foreach ($item['children'] as $child)knowing the argument is an array (you need to make sure that$item['children']exists and is an array).