Home >Backend Development >PHP Tutorial >How to add two arrays in php, how to add arrays in php_PHP tutorial
The example in this article describes how to add two arrays in PHP. Share it with everyone for your reference. The details are as follows:
Example 1:
<?php $arr1 = array("a"=>"朝阳区","b"=>"海淀区"); $arr2 = array("h"=>"西城区","a"=>"东城区","b"=>"丰台区"); $arr = $arr1 + $arr2; echo "<pre class="brush:php;toolbar:false">"; print_r($arr); ?>
The output results are as follows:
Array ( [a] => 朝阳区 [b] => 海淀区 [h] => 西城区 )
Change the order of addition, Example 2:
<?php $arr1 = array("a"=>"朝阳区","b"=>"海淀区"); $arr2 = array("h"=>"西城区","a"=>"东城区","b"=>"丰台区"); $arr = $arr2 + $arr1; echo "<pre class="brush:php;toolbar:false">"; print_r($arr); ?>
The output results are as follows:
Array ( [h] => 西城区 [a] => 东城区 [b] => 丰台区 )
Comparing the above two examples, we can see:
(1) The addition is the following array, which is added to the previous array;
(2) If the key names are the same, they will not be overwritten.
I hope this article will be helpful to everyone’s PHP programming design.