当前位置:Gxlcms > PHP教程 > PHP的arraymerge与+号的差别

PHP的arraymerge与+号的差别

时间:2021-07-01 10:21:17 帮助过:4人阅读

PHP 的 array merge 与 + 号的区别

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

范例 - 使用 array_merge

$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) 值保留.

范例 - 使用 + 合并

$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 .

人气教程排行