两个二维数组的合并

发布于 2022-09-01 17:45:46 字数 1402 浏览 16 评论 0

合并前的数组a:

array (size=3)
  0 => 
    array (size=2)
      'id' => string '113' (length=3)
      'email' => string 'yintx_1292342352@163.com' (length=24)
    
  1 => 
    array (size=2)
      'id' => string '111' (length=3)
      'email' => string 'yintx_1293456456@163.com' (length=24)
  2 => 
    array (size=2)
      'id' => string '109' (length=3)
      'email' => string 'yintx_129@99999.com' (length=19)

合并前的数组b:

array (size=2)
  0 => 
    array (size=2)
      'user_id' => string '113' (length=2)
      'count' => string '1' (length=1)
  1 => 
    array (size=2)
      'user_id' => string '109' (length=2)
      'count' => string '8' (length=1)

合并后的数组(合并条件,a的id==b的user_id时合并):

array (size=3)
  0 => 
    array (size=3)
      'id' => string '113' (length=3)
      'email' => string 'yintx_1292342352@163.com' (length=24)
      'count' => string '1' (length=1)
    
  1 => 
    array (size=3)
      'id' => string '111' (length=3)
      'email' => string 'yintx_1293456456@163.com' (length=24)
      'count' => string '0' (length=1)
  2 => 
    array (size=3)
      'id' => string '109' (length=3)
      'email' => string 'yintx_129@99999.com' (length=19)
      'count' => string '8' (length=1)

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

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

发布评论

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

评论(6

一场春暖 2022-09-08 17:45:46

array_merge_recursive() 函数与 array_merge() 函数 一样,将一个或多个数组的元素的合并起来,一个数组中的值附加在前一个数组的后面。并返回作为结果的数组。
但是,与 array_merge() 不同的是,当有重复的键名时,值不会被覆盖,而是将多个相同键名的值递归组成一个数组。(参见例子 1)

<?php
$a1=array("a"=>"Horse","b"=>"Dog");
$a2=array("c"=>"Cow","b"=>"Cat");
print_r(array_merge_recursive($a1,$a2));
?>
输出:
Array (
[a] => Horse
[b] => Array ( [0] => Dog [1] => Cat )
[c] => Cow
)

拔了角的鹿 2022-09-08 17:45:46
$a = array(
    array('id'=>'113','email'=>'yintx_1292342352@163.com'), 
    array('id'=>'111','email'=>'yintx_1293456456@163.com'), 
    array('id'=>'109','email'=>'yintx_129@99999.com')
    );

$b = array(
    array('user_id'=>'113','count'=>'1'), 
    array('user_id'=>'109','count'=>'8')
    );

foreach($a as $ka=>$va){
    foreach($b as $kb=>$vb){
        if($va['id'] == $vb['user_id']){
            $a[$ka]['count'] = $vb['count'];
        }
    }
}

var_dump($a);

----------

array (size=3)
  0 => 
    array (size=3)
      'id' => string '113' (length=3)
      'email' => string 'yintx_1292342352@163.com' (length=24)
      'count' => string '1' (length=1)
  1 => 
    array (size=2)
      'id' => string '111' (length=3)
      'email' => string 'yintx_1293456456@163.com' (length=24)
  2 => 
    array (size=3)
      'id' => string '109' (length=3)
      'email' => string 'yintx_129@99999.com' (length=19)
      'count' => string '8' (length=1)
┾廆蒐ゝ 2022-09-08 17:45:46

数组A($arrA)

array (size=3)
  0 => 
    array (size=2)
      'id' => string '113' (length=3)
      'email' => string 'yintx_1292342352@163.com' (length=24)
  1 => 
    array (size=2)
      'id' => string '111' (length=3)
      'email' => string 'yintx_1293456456@163.com' (length=24)
  2 => 
    array (size=2)
      'id' => string '109' (length=3)
      'email' => string 'yintx_129@99999.com' (length=19)

数组B($arrB)

array (size=2)
  0 => 
    array (size=2)
      'user_id' => string '113' (length=2)
      'count' => string '1' (length=1)
  1 => 
    array (size=2)
      'user_id' => string '109' (length=2)
      'count' => string '8' (length=1)

数组C($arrC)

array (size=2)
  0 => 
    array (size=2)
      'id' => string '113' (length=2)
      'count' => string '1' (length=1)
  1 => 
    array (size=2)
      'id' => string '109' (length=2)
      'count' => string '8' (length=1)

你的需求没有现成的函数可以满足,倒是可以使用array_walk()来遍历重组:
例如:

array_walk($arrA,function(&$item, $key, $prefix)use($arrB){
    foreach($arrB as $k=>$v){
        if($item['id']==$v['user_id']){
            $item['count'] = $v['count'];
        }
    }
});

实现!
另外:执行:array_replace_recursive($arrA,$arrB)得到:

array (size=3)
  0 => 
    array (size=3)
      'id' => string '113' (length=3)
      'email' => string 'yintx_1292342352@163.com' (length=24)
      'user_id' => string '113' (length=2)
      'count' => string '1' (length=1)
    
  1 => 
    array (size=3)
      'id' => string '111' (length=3)
      'email' => string 'yintx_1293456456@163.com' (length=24)
      'user_id' => string '109' (length=2)
      'count' => string '8' (length=1)
  2 => 
    array (size=3)
      'id' => string '109' (length=3)
      'email' => string 'yintx_129@99999.com' (length=19)

执行:array_replace_recursive($arrA,$arrC)得到:

array (size=3)
  0 => 
    array (size=3)
      'id' => string '113' (length=3)
      'email' => string 'yintx_1292342352@163.com' (length=24)
      'count' => string '1' (length=1)
  1 => 
    array (size=3)
      'id' => string '109' (length=3)
      'email' => string 'yintx_1293456456@163.com' (length=24)
      'count' => string '8' (length=1)
  2 => 
    array (size=3)
      'id' => string '109' (length=3)
      'email' => string 'yintx_129@99999.com' (length=19)
酒浓于脸红 2022-09-08 17:45:46

提供一个不使用循环的办法

$a = [
    ['id' => 1, 'age' => 18],
    ['id' => 2, 'age' => 19],
    ['id' => 3, 'age' => 20],
    ['id' => 4, 'age' => 22]
];

$b = [
    ['id' => 2, 'name' => 'aa'],
    ['id' => 4, 'name' => 'bb']
];

$a1 = array_column($a, null, 'id');
$b1 = array_column($b, null, 'id');

$keys = array_keys($a1);
$temp = array_fill_keys($keys, []);
$b1 = $b1 + $temp;
ksort($b1);
function merge($v1, $v2) {
    return array_merge($v1, $v2);
}
$new = array_map('merge', $a1, $b1);
print_r($new);
心碎的声音 2022-09-08 17:45:46

array_merge_recursive($a, $b);

长伴 2022-09-08 17:45:46

array_merge_recursive, 需要注意的是此函数合并的是相同的键名,而需求是合并相同的id, 所以首先要把id提取出来作为键名, 其次此函数只合并相同的字符串键名,所以还需要把键名转换为字符串,然后就可以使用array_merge_recursive合并了。

$a1 = $b1 = [];

foreach($a as $v){$a1['id_'.$v['id']]=$v;}

foreach($b as $v){$b1['id_'.$v['id']]=$v;}

print_r(array_values(array_merge_recursive($a1,$b1)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文