我有这个数组:
[@attributes] => Array
(
[url] =>
[navigation] => true
[template] => home
)
[@children] => Array
(
[0] => Array
(
[name] => Home
)
[1] => Array
(
[desc] => Lorem ipsum dolor
)
[2] => Array
(
[@children] => Array......
我需要它来代替:
[@attributes] => Array
(
[url] =>
[navigation] => true
[template] => home
)
[@children] => Array
(
[name] => Home
[desc] => Lorem ipsum dolor
[@children] => Array......
我无法改变这个数组实际构建的方式,所以我需要基本上摆脱每个项目在他们自己的数组中,它们都应该嵌套在子项下。
提前致谢
答案 0 :(得分:4)
您可以使用call_user_func_array
和array_merge
执行此操作:
$arr['@children'] = call_user_func_array('array_merge', $arr['@children']);
这将使用array_merge
数组作为参数调用$arr['@children']
。
答案 1 :(得分:2)
首先遍历子节点,将每个值存储在一个新数组中,然后覆盖它:
$newChildren = array();
foreach ($arr['@attributes']['@children'] as $property) {
foreach ($property as $key => $val) {
$newChildren[$key] = $val;
}
}
$arr['@attributes']['@children'] = $newChildren;
如果这些值的嵌套深度超过一个级别(我看到另一个@children键/数组),您可以将它放在一个函数中并递归调用它。
答案 2 :(得分:0)
尝试一下......
foreach (@attributes[@children] as $arrayVal)
{
foreach($arrayVal as $key=>$val)
@mySimpleArray[] = $val
}