Version
键名是string,两者区别
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?php $arr1 = ['a' => 'PHP']; $arr2 = ['a' => 'PYTHON']; print_r(array_merge($arr1, $arr2));
print_r($arr1 + $arr2);
$arr1 = ['a' => '1', 'b' => '2']; $arr2 = ['a' => '1', 'b' => '3', 'c' => '2']; print_r(array_merge($arr1,$arr2));
print_r($arr1 + $arr2);
|
键名是数字,两者区别
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?php $arr1 = ['JAVA', 'PHP']; $arr2 = ['PYTHON', 'PHP']; print_r(array_merge($arr1, $arr2));
print_r($arr1 + $arr2);
$arr1 = [1 => '1', 2 => '2']; $arr2 = [1 => '1', 2 => '3', 8 => '2']; $ret = array_merge($arr1,$arr2); print_r($ret);
print_r($arr1 + $arr2);
|
延伸
如何最有效的获取字符键名相同,值不同的两个数组值集合呢
1 2 3 4
| $arr1 = ['a' => 'PHP', 'C']; $arr2 = ['a' => 'JAVA', 'C', 'PYTHON']; print_r(array_merge_recursive($arr1, $arr2));
|