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

php实现两个数组相加的方法_php技巧

WBOY
Release: 2016-05-16 20:23:04
Original
1294 people have browsed it

本文实例讲述了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;>
Copy after login

输出结果如下:

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

改变相加的顺序,实例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;>
Copy after login

输出结果如下:

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

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

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

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

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