怎么合并两个数组的值?

需要合并数组1与数组2,数组2的只要有值就覆盖数组1的值,合并结果如图[3],请请请教各位大神看看
说明:数组1结构固定的,数组2结构不固定有时多几个字段 有时少。

数组1
$arr1 = Array(
    0 => Array('name' => '山东东营','Nab' =>12,'Nac' => '','Nad' => '','Nae' => '','Nag' => '','Nak' => '','Nao' => '','Naw' => '','Naz' =>'','province' => '海南省','Naaa' => 12),
    1 => Array('name' => '天津','Nab' => 3,'Nac' => '','Nad' => '','Nae' => '','Nag' => '','Nak' => '','Nao' => '','Naw' => '','Naz' =>'','province' => '湖北省','Naaa' => 2),
    2 => Array('name' => '洛阳','Nab' => 3,'Nac' => '','Nad' => '','Nae' => '','Nag' => '','Nak' => '','Nao' => '','Naw' => '','Naz' =>'','province' => '湖北省','Naaa' => 2)
    ...
);

$arr2 = Array(
    0 => Array('name' => '山东东营','Nac' => 1,'Nad' => 262,'Nae' => 1,'Nag' => 1,'Nak' => 524,'Nao' => 2,'Naw' => 2,'Naz' => 1),
    1 => Array('name' => '天津','Nac' => 0,'Nad' =>'' ,'Nae' => 0,'Nao' => 0),
    2 => Array('name' => '洛阳','Nac' => 1,'Nad' => 342,'Nae' => 1,'Nag' => 1,'Nak' => 342,'Nao' => 1)
    ...
   );

问题补充
图[1] 基本结构,图[2] 数据,图[3] 最终结果
如图:
https://img2.mukewang.com/5c8f615c0001842808000682.jpg

红糖糍粑
浏览 544回答 6
6回答

慕码人2483693

$数组3 = array_merge($数组1, $数组2);

天涯尽头无女友

如果两个数组的key不会重复的话,可以声明一个新数组再存进去。效率貌似比较差,期待更优解。 $arr1 = [ 0 => [ 'name' => '山东东营', 'Nab' => 12, 'Nac' => '', 'Nad' => '', 'Nae' => '', 'Nag' => '', 'Nak' => '', 'Nao' => '', 'Naw' => '', 'province' => '海南省', 'Naaa' => 12 ], 1 => [ 'name' => '天津', 'Nab' => 12, 'Nac' => '', 'Nad' => '', 'Nae' => '', 'Nag' => '', 'Nak' => '', 'Nao' => '', 'Naw' => '', 'province' => '天津', 'Naaa' => 12 ], 3 => [ 'name' => '洛阳', 'Nab' => 12, 'Nac' => '', 'Nad' => '', 'Nae' => '', 'Nag' => '', 'Nak' => '', 'Nao' => '', 'Naw' => '', 'province' => '湖北省', 'Naaa' => 12 ], ]; $arr2 = [ 0 => [ 'name' => '山东东营', 'Nab' => 12, 'Nac' => '', 'Nad' => '', 'Nae' => '', 'Nag' => '', 'Nak' => '', 'Nao' => '', 'Naw' => '', 'province' => '海南省', 'Naaa' => 12 ], 1 => [ 'name' => '天津', 'Nab' => 12, 'Nac' => '', 'Nad' => '', 'Nae' => '', 'Nag' => '', 'Nak' => '', 'Nao' => '', 'Naw' => '', 'province' => '天津', 'Naaa' => 12 ], 2 => [ 'name' => '帝都', 'Nab' => 12, 'Nac' => '', 'Nad' => '', 'Nae' => '', 'Nag' => '', 'Nak' => '', 'Nao' => '', 'Naw' => '', 'province' => '湖北省', 'Naaa' => 12 ], ]; //声明一个空数组 $resArr = []; foreach ($arr1 as $k => $a) { $resArr[$k]=$a; } foreach ($arr2 as $k => $b) { $resArr[$k]=$b; } ksort($resArr); echo '<pre>'; var_dump($resArr);

慕勒3428872

header("Content-type:text/html;charset=utf-8"); $arr1 = Array( Array( 'name' => '山东东营', 'Nab' =>12, 'Nac' => '', 'Nad' => '', 'Nae' => '', 'Nag' => '', 'Nak' => '', 'Nao' => '1', 'Naw' => '', 'province' => '海南省', 'Naaa' => 12), Array( 'name' => '天津', 'Nab' => 3, 'Nac' => '', 'Nad' => '', 'Nae' => '', 'Nag' => '', 'Nak' => '', 'Nao' => '', 'Naw' => '', 'province' => '湖北省', 'Naaa' => 2), Array( 'name' => '洛阳', 'Nab' => 3, 'Nac' => '', 'Nad' => '', 'Nae' => '', 'Nag' => '', 'Nak' => '', 'Nao' => '', 'Naw' => '', 'province' => '湖北省', 'Naaa' => 2), ); $arr2 =Array( Array( 'name' => '山东东营', 'Nab' =>12, 'Nac' => 1, 'Nad' => 262, 'Nae' => 1, 'Nag' => 1, 'Nak' => 524, 'Nao' =>'', 'Naw' => 2, 'province' => '海南省', 'Naaa' => 12), Array( 'name' => '天津', 'Nab' => 3, 'Nac' => 0, 'Nad' => '', 'Nae' => 0, 'Nag' => '', 'Nak' => '', 'Nao' => '', 'Naw' => '', 'province' => '湖北省', 'Naaa' => 2), Array( 'name' => '洛阳', 'Nab' => 3, 'Nac' => 1, 'Nad' => 342, 'Nae' => 1, 'Nag' => 0, 'Nak' => 242, 'Nao' => '', 'Naw' => '', 'province' => '湖北省', 'Naaa' => 2), ); $result = [] ; $data = []; $data = array_keys($arr2[0]);//获取数组2下标 foreach ($arr1 as $k => $v) { $result [$k] =$v; } foreach ($arr2 as $k => $val) { foreach ($data as $key => $value){ if($val[$value] !== ''){ $result [$k][$value] = $val[$value]; }else{ } } } ksort($result); var_dump($result);

忽然笑

遍历循环,相同键值合并。

万千封印

<?php $arr1 = Array( Array( 'name' => '山东东营', 'Nab' =>12, 'Nac' => '', 'Nad' => '', 'Nae' => '', 'Nag' => '', 'Nak' => '', 'Nao' => '1', 'Naw' => '', 'province' => '海南省', 'Naaa' => 12), Array( 'name' => '天津', 'Nab' => 3, 'Nac' => '', 'Nad' => '', 'Nae' => '', 'Nag' => '', 'Nak' => '', 'Nao' => '', 'Naw' => '', 'province' => '湖北省', 'Naaa' => 2), Array( 'name' => '洛阳', 'Nab' => 3, 'Nac' => '', 'Nad' => '', 'Nae' => '', 'Nag' => '', 'Nak' => '', 'Nao' => '', 'Naw' => '', 'province' => '湖北省', 'Naaa' => 2), ); $arr2 =Array( Array( 'name' => '山东东营', 'Nab' =>12, 'Nac' => 1, 'Nad' => 262, 'Nae' => 1, 'Nag' => 1, 'Nak' => 524, 'Nao' =>'', 'Naw' => 2, 'province' => '海南省', 'Naaa' => 12), Array( 'name' => '天津', 'Nab' => 3, 'Nac' => 0, 'Nad' => '', 'Nae' => 0, 'Nag' => '', 'Nak' => '', 'Nao' => '', 'Naw' => '', 'province' => '湖北省', 'Naaa' => 2), Array( 'name' => '洛阳', 'Nab' => 3, 'Nac' => 1, 'Nad' => 342, 'Nae' => 1, 'Nag' => 0, 'Nak' => 242, 'Nao' => '', 'Naw' => '', 'province' => '湖北省', 'Naaa' => 2), ); $arr3 = $arr2+$arr1; foreach ($arr3 as $k => $v){ if(empty($arr2[$k])){ $arr2[$k] = []; } $arr3[$k] = $arr3[$k]+$arr2[$k]; } echo '<pre>'; print_r($arr3);
打开App,查看更多内容
随时随地看视频慕课网APP