Home >Backend Development >PHP Tutorial >php实现两个数组相加的方法_PHP

php实现两个数组相加的方法_PHP

WBOY
WBOYOriginal
2016-05-31 13:16:581984browse

本文实例讲述了php实现两个数组相加的方法。分享给大家供大家参考。具体如下:

实例1:

<&#63;php
$arr1 = array("a"=>"朝阳区","b"=>"海淀区");
$arr2 = array("h"=>"西城区","a"=>"东城区","b"=>"丰台区");
$arr = $arr1 + $arr2;
echo "<pre class="brush:php;toolbar:false">";
print_r($arr);
&#63;>

输出结果如下:

Array
(
  [a] => 朝阳区
  [b] => 海淀区
  [h] => 西城区
)

改变相加的顺序,实例2:

<&#63;php
$arr1 = array("a"=>"朝阳区","b"=>"海淀区");
$arr2 = array("h"=>"西城区","a"=>"东城区","b"=>"丰台区");
$arr = $arr2 + $arr1;
echo "<pre class="brush:php;toolbar:false">";
print_r($arr);
&#63;>

输出结果如下:

Array
(
  [h] => 西城区
  [a] => 东城区
  [b] => 丰台区
)

从上面两个实例比较,可以看出:

(1)相加是后面一个数组,加入到前面一个数组中;
(2)键名相同时,不会被覆盖。

希望本文所述对大家的php程序设计有所帮助。

Statement:
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