本文介绍了PHP的节点移动到父数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在不设置字段的情况下将字段数组的所有节点移到其父数组 113?
How could I move all nodes of the 'fields' array into its parent array '113', whilst unsetting 'fields' ?
[a] => Array
(
[113] => Array
(
[title] => asdfasdfas
[alias] => asdfasdfas
[fields] => Array
(
[jr_streetaddress] => Array
(
[type] => text
[label] => Street Address
[data] => asdfasdffsd
)
[jr_towncity] => Array
(
[type] => text
[label] => Town / City
[data] => Nottingham
)
)
)
)
推荐答案
假定您的顶级数组( $ something ['a']
)是变量 $ a
:
Assuming your top level array ($something['a']
) is the variable $a
:
foreach($a as $key => $values){
if(isset($values['fields']))
{
$a[$key] = array_merge($a[$key], (array) $values['fields']);
unset($a[$key]['fields']);
}
}
或者,如果您不想击中每个数组元素在 $ a
中,您可以删除循环并用 $ a [113]代替
和 $ values
] $ key
和 113
。
Alternatively, if you dont want to hit every array element in $a
you can just remove the loop and substitute $values
with $a[113]
and $key
with 113
.
还要注意将fields元素强制转换为数组,如果不是(array)$ values ['fields']
Also note the casting for the fields element to an array, jsut in case it isnt one with (array) $values['fields']
这篇关于PHP的节点移动到父数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!