The difference between PHP merge array array_merge function operator plus sign and - PHP tutorial

WBOY
Release: 2016-07-21 15:49:19
Original
1234 people have browsed it

The description of array_merge in the reference manual is as follows:
array_merge() Merges the cells of two or more arrays, and the values ​​in one array are appended to the previous array. Returns the resulting array.
If there is the same string key name in the input array, the value after the key name will overwrite the previous value. However, if the array contains numeric keys, the subsequent values ​​will not overwrite the original values ​​but will be appended to them.
The difference between the two is:
1. When the array key name is a numeric key name, and the two arrays to be merged have numeric KEYs with the same name, using array_merge() will not overwrite the original value. Using "+" to merge arrays will return the first appearing value as the final result, and "discard" those values ​​with the same key name in subsequent arrays (note: not overwriting but retaining the first appearing value) . Example:

Copy code The code is as follows:

$array1 = array(1=>'0');
$array2 = array(1=> "data");
$result1 = $array2 + $array1;/*The result is the value of $array2*/
print_r($result);
$ result = $array1 + $array2 ;/*The result is the value of $array1*/
print_r($result);
$result3 = array_merge($array2,$array1);/*The result is $array2 and $ The value of array1, the key name is reassigned*/
print_r($result3);
$result4 = array_merge($array1,$array2);/*The result is the value of $array1 and $array2, the key name is Reassign*/
print_r($result4);

The output result is:
Copy code The code is as follows:

Array
(
[1] => data
)
Array
(
[1] => 0
)
Array
(
[0] => data
[1] => 0
)
Array
(
[0] => 0
[1] => data
)

 2. When the same array key name is a character, the "+" operator is the same as when the key name is a number, but array_merge() At this time, the previous value with the same key name will be overwritten.
Example:
');
$array2 = array('asd' => "data"); $result1 = $array2 + $array1;/*The result is the value of $array2*/ print_r($ result); $result = $array1 + $array2 ;/*The result is the value of $array1*/ print_r($result);
$result3 = array_merge($array2,$array1);/ *The result is $array1*/
print_r($result3);
$result4 = array_merge($array1,$array2);/*The result is $array2*/
print_r($result4);


The output result is:



Copy code

The code is as follows:


Array
( [asd] => data ) Array (
[asd] => 0
)
Array
(
[asd] => ; 0
)
Array
(
[asd] => data
)





http://www.bkjia.com/PHPjc/319608.html

www.bkjia.com
true

http: //www.bkjia.com/PHPjc/319608.html

array_merge is described in the reference manual as follows: array_merge() Merges the cells of two or more arrays, The values ​​in one array are appended to the previous array. Return as result...
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