为了账号安全,请及时绑定邮箱和手机立即绑定

怎么合并两个数组的值?

怎么合并两个数组的值?

PHP
红糖糍粑 2019-03-04 23:12:25
需要合并数组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] 最终结果如图:
查看完整描述

6 回答

?
慕码人2483693

TA贡献1860条经验 获得超9个赞

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

查看完整回答
反对 回复 2019-03-18
?
天涯尽头无女友

TA贡献1831条经验 获得超9个赞

如果两个数组的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);
查看完整回答
反对 回复 2019-03-18
?
慕勒3428872

TA贡献1848条经验 获得超6个赞

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);
查看完整回答
反对 回复 2019-03-18
?
忽然笑

TA贡献1806条经验 获得超5个赞

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

查看完整回答
反对 回复 2019-03-18
?
万千封印

TA贡献1891条经验 获得超3个赞

<?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);
查看完整回答
反对 回复 2019-03-18
  • 6 回答
  • 0 关注
  • 543 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信