$arr1 + $arr2
键名出现重复,不管是数字还是字符,统一使用首次出现的,丢弃后面出现的(前面覆盖后面)。
array_merge(arr1,arr2)
- 如果是字符,则使用后面出现的,丢弃前面出现的(后面覆盖前面)
- 如果是数字则全部保留。
索引数组
$arr1 = [1 => 'one', 3 => 'three'];
$arr2 = [1 => 'new one', 6 => 'six'];
$result1 = array_merge($arr1, $arr2);
$result2 = $arr1 + $arr2;
//output
$result1:
Array
(
[0] => one
[1] => three
[2] => new one
[3] => six
)
$result2:
Array
(
[1] => one
[3] => three
[6] => six
)
关联数组
$arr3 = ['name' => '张三', 'age' => 21];
$arr4 = ['name' => '李四', 'age' => 22];
$result3 = array_merge($arr3, $arr4);
$result4 = $arr3 + $arr4;
dd($result3);
dd($result4);
//output
//result3:
Array
(
[name] => 李四
[age] => 22
)
//result4:
Array
(
[name] => 张三
[age] => 21
)