@Chiang
2020-08-29T17:29:33.000000Z
字数 460
阅读 543
2020-08
工作总结
我们有
$a
,$b
两个数组; 我们以$a
为参考点
当count($b)
>count($a)
时 我们把$a
和$b
不对应的元素取出来添加到$a
数组中,组成新的数组$c
;其他情况直接取$a
即可.
$a = [
['key' => 1, 'val' => 'a值1'],
['key' => 2, 'val' => 'a值2'],
];
$b = [
['key' => 1, 'val' => 'b值1'],
['key' => 2, 'val' => 'b值2'],
['key' => 3, 'val' => 'b值3'],
];
$newA = array_column($a, null, 'key');
$newB = array_column($b, null, 'key');
$diffArr = array_diff_key($newB, $newA);
foreach ($diffArr as &$value) {
$value['val'] = '';
}
$c = array_merge($a, $diffArr);
参考资料:
array_diff_key