How to add two arrays in php_PHP tutorial

WBOY
Release: 2016-07-13 10:04:06
Original
2152 people have browsed it

How to add two arrays in php

This article mainly introduces how to add two arrays in php, and analyzes the array operator + in php with examples. The usage skills have certain reference value. Friends in need can refer to it

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:

?

1

2

3

4

5

6

7

$arr1 = array("a"=>"朝阳区","b"=>"海淀区");

$arr2 = array("h"=>"西城区","a"=>"东城区","b"=>"丰台区");

$arr = $arr1 + $arr2;

echo "

";

print_r($arr);

?>

1 2

3

4

5

6

1

2

3

4

5

6

Array

(

[a] => 朝阳区

[b] => 海淀区

[h] => 西城区

)

7

$arr1 = array("a"=>"Chaoyang District","b"=>"Haidian District");

$arr2 = array("h"=>"Xicheng District","a"=>"Dongcheng District","b"=>"Fengtai District");

$arr = $arr1 + $arr2;

1

2

3

4

5

6

7

$arr1 = array("a"=>"朝阳区","b"=>"海淀区");

$arr2 = array("h"=>"西城区","a"=>"东城区","b"=>"丰台区");

$arr = $arr2 + $arr1;

echo "

";

print_r($arr);

?>

echo "
";
            print_r($arr);

?>

1

2

3

4

5

6

Array

(

[h] => 西城区

[a] => 东城区

[b] => 丰台区

)

The output results are as follows:

?


1

2

3 4

56
Array ( [a] => Chaoyang District [b] => Haidian District [h] => Xicheng District )
Change the order of addition, Example 2: ?
1 2 3 4 5 6 7 <🎜>$arr1 = array("a"=>"Chaoyang District","b"=>"Haidian District"); $arr2 = array("h"=>"Xicheng District","a"=>"Dongcheng District","b"=>"Fengtai District"); $arr = $arr2 + $arr1; echo "
";
            print_r($arr);
            ?>
            
            
The output results are as follows: ?
1 2 3 4 5 6 Array ( [h] => Xicheng District [a] => Dongcheng District [b] => Fengtai District )
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. http://www.bkjia.com/PHPjc/966911.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/966911.htmlTechArticleHow to add two arrays in php. This article mainly introduces how to add two arrays in php. , the example analyzes the usage skills of PHP's array operator +, which has certain reference value...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!