Rumah > Artikel > pembangunan bahagian belakang > PHP函数array_merge()与加号运算符的不同之处_PHP教程
我们今天为大家介绍的是array_merge在参考手册中的说明如下:
PHP函数array_merge()将两个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。
如果输入的数组中有相同的字符串键名,则该键名后面的值将覆盖前一个值。然而,如果数组包含数字键名,后面的值将不会覆盖原来的值,而是附加到后面。
PHP函数array_merge()与加号运算符两个的区别是:
1.数组键名为数字键名时,要合并的两个数组中有同名数字KEY的时候,使用array_merge()不会覆盖掉原来的值,而使用“+”合并数组则会 把最先出现的值作为最终结果返回,而把后面的数组拥有相同键名的那些值“抛弃”掉(注意:不是覆盖而是保留最先出现的那个值)。例子:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute-value"><font color="#0000ff">array</font></span><span class="attribute"><font color="#ff0000">array1</font></span><span> = array(</span><span class="attribute"><font color="#ff0000">1</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'0'); </span></span></li> <li class=""> <span>$</span><span class="attribute-value"><font color="#0000ff">array</font></span><span class="attribute"><font color="#ff0000">array2</font></span><span> = array(</span><span class="attribute"><font color="#ff0000">1</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "data"); </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">result1</font></span><span> = $array2 + $array1;/*结果为$array2的值*/ </span> </li> <li class=""><span>print_r($result); </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">result</font></span><span> = $array1 + $array2 ;/*结果为$array1的值*/ </span> </li> <li class=""><span>print_r($result); </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">result3</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array_merge</font></span><span>($array2,$array1);/*结果为$array2和$array1的值,键名被重新分配*/ </span> </li> <li class=""><span>print_r($result3); </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">result4</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array_merge</font></span><span>($array1,$array2);/*结果为$array1和$array2的值,键名被重新分配*/ </span> </li> <li class=""><span>print_r($result4); </span></li> </ol>
输出结果为:
<ol class="dp-xml"> <li class="alt"><span><span>Array ( [1] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> data ) </span></span></li> <li class=""> <span>Array ( [1] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 0 ) </span> </li> <li class="alt"><span>Array ( </span></li> <li class=""> <span>[0] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> data </span> </li> <li class="alt"> <span>[1] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 0 </span> </li> <li class=""><span>) </span></li> <li class="alt"><span>Array </span></li> <li class=""><span>( </span></li> <li class="alt"> <span>[0] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 0 </span> </li> <li class=""> <span>[1] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> data </span> </li> <li class="alt"><span>) </span></li> </ol>
2.当相同数组键名为字符时,“+”运算符与键名为数字时一样,但PHP函数array_merge()此时会覆盖掉前面相同键名的值。
例子:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute-value"><font color="#0000ff">array</font></span><span class="attribute"><font color="#ff0000">array1</font></span><span> = array('asd'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'0'); </span></span></li> <li class=""> <span>$</span><span class="attribute-value"><font color="#0000ff">array</font></span><span class="attribute"><font color="#ff0000">array2</font></span><span> = array('asd' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "data"); </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">result1</font></span><span> = $array2 + $array1;/*结果为$array2的值*/ </span> </li> <li class=""><span>print_r($result); </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">result</font></span><span> = $array1 + $array2 ;/*结果为$array1的值*/ </span> </li> <li class=""><span>print_r($result); </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">result3</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array_merge</font></span><span>($array2,$array1);/*结果为$array1*/ </span> </li> <li class=""><span>print_r($result3); </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">result4</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array_merge</font></span><span>($array1,$array2);/*结果为$array2*/ </span> </li> <li class=""><span>print_r($result4); </span></li> </ol>
输出结果为:
<ol class="dp-xml"> <li class="alt"><span><span>Array ( [asd] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> data ) </span></span></li> <li class=""> <span>Array ( [asd] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 0 ) </span> </li> <li class="alt"> <span>Array ( [asd] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 0 ) </span> </li> <li class=""> <span>Array ( [asd] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> data ) </span> </li> </ol>
以上就是PHP函数array_merge()与加号运算符在实际使用中的不同之处。