我有一个关于嵌套数组的问题;我在谷歌和stackoverflow上搜索了一下,但找不到符合我需求的解决方案:-/
我有以下数组:
Array
(
[0] => Array
(
[name] => Europe
[children] => Array
(
[0] => West
[1] => East
[2] => South
[3] => North
[4] => Zimbabwe
)
)
[1] => Array
(
[name] => West
[children] => Array
(
[0] => Germany
[1] => France
[2] => Switzerland
[3] => Netherlands
[4] => Belgium
[5] => Luxembourg
[6] => United Kingdom
[7] => Ireland
)
)
[2] => Array
(
[name] => Germany
[children] =>
)
[3] => Array
(
[name] => France
[children] =>
)
)
我想把它转换成嵌套的,意思是:
Array
(
[0] => Array
(
[name] => Europe
[children] => Array
(
[0] => Array
(
[name] => West
[children] => Array
(
[0]=> Array
(
[name] => Germany
)
[1]=> Array
(
[name] => France
)
)
)
[2] => Array
(
[name] => East
[children] => Array
(
[0]=> Array
(
[name] => Poland
)
[1]=> Array
(
[name] => Austria
)
)
)
)
)
)
你有主意吗?
提前感谢!
foreach ($unnested_ary as $one_k => $one_v)
{
foreach ($one_v as $two_k => $two_v)
{
if ($two_k == 'children')
{
foreach ($two_v as $three_k => $three_v)
{
$unnested_array[$one_k][$two_k][$three_k][] = array(array(
'name' => $three_v
));
}
}
}
}
我不确定我是否正确地嵌套了它,但你应该了解这个想法,并使用这个代码。这基本上应该像您要求的那样创建新的键和值。