Home > Backend Development > PHP Tutorial > PHP 的 array merge 与 + 号的差别

PHP 的 array merge 与 + 号的差别

WBOY
Release: 2016-06-13 13:01:50
Original
826 people have browsed it

PHP 的 array merge 与 + 号的区别

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

范例 - 使用 array_merge

<?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
)
原文网站 / 转载自: Tsung Hao

This work, unless otherwise expressly stated, is licensed under a
Creative Commons Attribution-ShareAlike 3.0 Unported License .

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template