日期:2014-05-17  浏览次数:20508 次

PHP 的 array merge 与 + 号的区别

PHP 的 array_merge 会将 数值 变成 0, 1, 2..., 就算是强制转换成字串也一样.

范例 - 使用 array_merge

<?php
$a1 = array(
??? '9' => '0',
??? '311' => '1',
??? '快乐' => '2',
??? '2009a' => '3');

$a2 = array(
??? '2009' => '11',
??? '圣诞节' => '22',
??? '111a' => '33');

$amerge = array();

$amerge = array_merge($a1, $a2);

print_r($amerge);
?>

输出结果

Array
(
??? [0] => 0
??? [1] => 1
??? [快乐] => 2
??? [2009a] => 3
??? [2] => 11
??? [圣诞节] => 22
??? [111a] => 33
)

于 官方网站查(array_merge ), 使用 "+" 就可以 merge, 而且会将 key(hash、index) 值保留.

范例 - 使用 + 合并

<?php
$a1 = array(
??? '9'???? => '0',
??? '311'?? => '1',
??? '快乐'?? => '2',
??? '2009a' => '3'
);

$a2 = array(
??? '2009'? => '11',
??? '圣诞节' => '22',
??? '111a'? => '33'
);

$amerge = array();

$amerge = $a1 + $a2;

print_r($amerge);
?>

输出结果

Array
(
??? [9] => 0
??? [311] => 1
??? [快乐] => 2
??? [2009a] => 3
??? [2009] => 11
??? [圣诞节] => 22
??? [111a] => 33
)
原文网站 / 转载自: Tsung Hao
This work, unless otherwise expressly stated, is licensed under a
Creative Commons Attribution-ShareAlike 3.0 Unported License .