Array Funcitons
array_merge
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$array3 = array(0, 1, 2, 3, 4);
$array4 = array(5, 6, 7, 8, 9);
// $ret1 = array_merge($array1, $array2);
// print_r($ret1);
// $ret11 = $array1 + $array2;
// print_r($ret11);
// $ret2 = array_merge($array1, array("color" => null));
// print_r($ret2);
// $ret3 = array_merge($array3, $array4);
// print_r($ret3);
// $ret4 = array_merge($array3, $array4);
// print_r($ret4);
// $ret5 = $array3 + $array4;
// print_r($ret5);
$ret6 = array() + $array3 ;
print_r($ret6);
// $ret7 = array_merge($array3, array());
// print_r($ret7);
array_merge當(dāng)key值為索引數(shù)組時(shí),array不合并,顯示如下
//ret4的結(jié)果
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 8
[9] => 9
)
當(dāng)為 +
號(hào)時(shí),ret5顯示結(jié)果(取前值,后值被覆蓋)
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
)
array_merge當(dāng)下標(biāo)為數(shù)字時(shí)碗旅,不合并下標(biāo)相同的值渡处,
+
:關(guān)聯(lián)數(shù)組和索引數(shù)組都是前值覆蓋后值,即取前值
array_merge
:索引數(shù)組不合并祟辟,關(guān)聯(lián)數(shù)組后值覆蓋前值医瘫,即取后值
array_merge
或者+
合并數(shù)組時(shí),不能與null進(jìn)行合并旧困,因?yàn)闀?huì)warning
PHP Warning: array_merge(): Expected parameter 2 to be an array, null given in ~/Notes/PHP/codes/array_merge.php on line 31