怎么合并两个数组的值?

发布于 2022-09-06 04:01:24 字数 1363 浏览 35 评论 0

需要合并数组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] 最终结果
如图:
图片描述

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

鲸落 2022-09-13 04:01:24

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

·深蓝 2022-09-13 04:01:24

如果两个数组的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);
我只土不豪 2022-09-13 04:01:24
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);
孤城病女 2022-09-13 04:01:24
用PHP的函数array_merge()就可以了

http://php.net/manual/zh/function.array-merge.php
面如桃花 2022-09-13 04:01:24

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

痴骨ら 2022-09-13 04:01:24
<?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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文