<?php
$arr1 = array('a'=>'PHP');
$arr2 = array('a'=>'PYTHON');
print_r(array_merge($arr1,$arr2)); //Array ( [a] => PYTHON ) 如果鍵名為字元,且鍵名相同,array_merge()後面陣列元素值會覆蓋前面陣列元素值
print_r($arr1+$arr2); //Array ( [a] => PHP )如果鍵名為字元,且鍵名相同,陣列相加會將最先出現的值作為結果
//再比如
$arr1 = array('a'=>'1','b'=>'2');
$arr2 = array('a'=>'1','b'=>'3','c'=>'2');
$ret = array_merge($arr1,$arr2);
print_r($ret);//Array ( [a] => 1 [b] => 3 [c] => 2 )
$ret = $arr1+$arr2;
print_r($ret);//Array ( [a] => 1 [b] => 2 [c] => 2 )
<?php
$arr1 = array("JAVA","PHP");
$arr2 = array("PYTHON","PHP");
print_r(array_merge($arr1,$arr2));//Array ( [0] => JAVA [1] => PHP [2] => PYTHON [3] => PHP ) 如果鍵名為數字,array_merge()不會進行覆蓋
print_r($arr1+$arr2);//Array ( [0] => JAVA [1] => PHP )如果鍵名為數字,陣列相加會將最先出現的值作為結果,後面鍵名相同的會被拋棄
//再比如
$arr1 = array(1=>'1',2=>'2');
$arr2 = array(1=>'1',2=>'3',8=>'2');
$ret = array_merge($arr1,$arr2);
print_r($ret);//Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 3 [4] => 2 ) 如果陣列中的鍵名為數字時,會格式化鍵名並保留所有鍵值。
//再比如
$arr1 = array(1,2);
$arr2 = array(1,2,8);
$ret = array_merge($arr1,$arr2);
print_r($ret);//Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 2 [4] => 8 )
?>
如何最有效的獲取字元鍵名相同值不同的兩個陣列值集合呢
$arr1 = array("a"=>"PHP","C"); $arr2 = array("a"=>"JAVA","C","PYTHON"); print_r(array_merge_recursive($arr1, $arr2)); //Array ( [a] => Array ( [0] => PHP [1] => JAVA ) [0] => C [1] => C [2] => PYTHON )