我有多维数组的这种组合,但是我无法在屏幕上输出正确的值,
$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'
)
)
)
);
有些数组本身,有些是 sub 数组,有些子数组有 subsub 数组。
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/>';
}
}
}
错误消息,
Warning: Invalid argument supplied for foreach() in -> foreach($child as $grandchild)
我要找的结果是这样的,
parent - Home
parent - Categories
child - Post
parent - Pages
child - Add
grandchild - Manage
修改
我尝试过以下测试,
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']);
}
}
}
}
}
但是我得到了一个奇怪的结果,
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.
答案 0 :(得分:0)
在foreach($item as $child)
上运行$parent[1]
时,$child
会采用以下值:'home'
,'Home'
和数组。
如何保持数组纯粹是关联的?
$parent = array(
array(
'function_name' => 'categories',
'screen_name' => 'Categories',
'children' => array(
array(
'function_name' => 'post',
'screen_name' => 'Post'
),
array(
'function_name' => 'review',
'screen_name' => 'Review',
'children' => array(
...
)
),
)
)
);
这样你可以运行foreach ($item['children'] as $child)
知道参数是一个数组(你需要确保$item['children']
存在并且是一个数组)。