我有一个可能看起来像的数组
$arr = array( array( 'test1' => 'testing1' ),array( 'test2' => array( 1 =>'testing2 ) );
我想把它变成
$newArr = array( 'test1' => 'testing1','test2' => array( 1 => 'testing2' ) );
所以我一直试图将所有数组元素上移一级.
eidt:
这是我将2个数组组合在一起的方法:
public function arrayMerge($arr1,$arr2) { foreach($arr2 as $key => $value) { $condition = (array_key_exists($key,$arr1) && is_array($value)); $arr1[$key] = ($condition ? $this->arrayMerge($arr1[$key],$arr2[$key]) : $value); } return $arr1; }
尝试
原文链接:https://www.f2er.com/php/138804.html$arr = array( array('test1' => 'testing1' ),array('test2' => array(1 =>'testing2')) ); $new = array(); foreach($arr as $value) { $new += $value; } var_dump($new);
产量
array 'test1' => string 'testing1' (length=8) 'test2' => array 1 => string 'testing2' (length=8)