PHP 的 array merge 与 + 号的区别
PHP 的 array_merge 会将 数值 变成 0, 1, 2..., 就算是强制转换成字串也一样.
<?php <br>
$a1 = array(<br>
??? '9' => '0',<br>
??? '311' => '1',<br>
??? '快乐' => '2',<br>
??? '2009a' => '3');<br><br>
$a2 = array(<br>
??? '2009' => '11',<br>
??? '圣诞节' => '22',<br>
??? '111a' => '33');<br><br>
$amerge = array();<br><br><strong>
$amerge = array_merge($a1, $a2);</strong>
<br><br>
print_r($amerge);<br>
?>
输出结果
Array
(
??? [0] => 0
??? [1] => 1
??? [快乐] => 2
??? [2009a] => 3
??? [2] => 11
??? [圣诞节] => 22
??? [111a] => 33
)
于 官方网站查(array_merge ), 使用 "+" 就可以 merge, 而且会将 key(hash、index) 值保留.
<?php <br>
$a1 = array(<br>
??? '9'???? => '0',<br>
??? '311'?? => '1',<br>
??? '快乐'?? => '2',<br>
??? '2009a' => '3'<br>
);<br><br>
$a2 = array(<br>
??? '2009'? => '11',<br>
??? '圣诞节' => '22',<br>
??? '111a'? => '33'<br>
);<br><br>
$amerge = array();<br><br><strong>
$amerge = $a1 + $a2;</strong>
<br><br>
print_r($amerge);<br>
?>
输出结果
Array
(
??? [9] => 0
??? [311] => 1
??? [快乐] => 2
??? [2009a] => 3
??? [2009] => 11
??? [圣诞节] => 22
??? [111a] => 33
)
This work, unless otherwise expressly stated, is licensed under a
Creative Commons Attribution-ShareAlike 3.0 Unported License
.