-> 数组相加:
类似于补充的效果,如果存在相同的键,那么后边的舍弃,不同则添加。
-> 数组array_merge:
类似于合并效果,针对数值键会从0重新给索引(详见示例二),如果是相同的字符键,后边的值会覆盖前边的值。
请看程序验证:
示例一:
$arr1 = [
'索1',
'索2',
'a' => 'ARR_1',
'b' => 'B',
'c' => 'C',
];
$arr2 = [
'数1',
'a' => 'ARR_2',
'd' => 'D',
];
// 数组直接相加
$arr = $arr1 + $arr2;
print_r($arr);
$arr = $arr2 + $arr1;
print_r($arr);
// 数组函数array_merge
$arr = array_merge($arr1, $arr2);
print_r($arr);
$arr = array_merge($arr2, $arr1);
print_r($arr);
示例二:
$arr1 = [
'索1',
'a' => 'ARR_1',
200 => 888,
];
$arr2 = [
'数1',
'd' => 'D',
100 => 666,
300 => 666,
500 => 888,
];
// 数组直接相加
$arr = $arr1 + $arr2;
print_r($arr);
$arr = $arr2 + $arr1;
print_r($arr);
// 数组函数array_merge
$arr = array_merge($arr1, $arr2);
print_r($arr);
$arr = array_merge($arr2, $arr1);
print_r($arr);
结果:
Array
(
[0] => 索1
[a] => ARR_1
[200] => 888
[d] => D
[100] => 666
[300] => 666
[500] => 888
)
Array
(
[0] => 数1
[d] => D
[100] => 666
[300] => 666
[500] => 888
[a] => ARR_1
[200] => 888
)
Array
(
[0] => 索1
[a] => ARR_1
[1] => 888
[2] => 数1
[d] => D
[3] => 666
[4] => 666
[5] => 888
)
Array
(
[0] => 数1
[d] => D
[1] => 666
[2] => 666
[3] => 888
[4] => 索1
[a] => ARR_1
[5] => 888
)