Detailed example of the difference between php merging arrays using + operator and array function array_merge

伊谢尔伦
Release: 2023-03-10 22:04:01
Original
1476 people have browsed it

PHP mergeArray Generally there are two methods, one is to directly use the plus sign to add, the other is to use the array_mergefunction Plus, 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.

There is a slight difference between the two:

#When the array key name is a numeric key name, the two arrays to be merged have numbers with the same name. When using KEY, using array_merge() will not overwrite the original value, but using "+" to merge arrays will return the first value as the final result, and "discard" those values ​​in the subsequent arrays that have the same key name. "Off (note: not overwriting but retaining the value that appears first). 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() will overwrite the previous value of the same key name.

Example:

<?php
$array1 = array(1=>&#39;0&#39;); 
$array2 = array(1=> "data"); 
$result1 = $array2 + $array1;/*结果为$array2的值*/ 
print_r($result); 
$result = $array1 + $array2 ;/*结果为$array1的值*/ 
print_r($result); 
$result3 = array_merge($array2,$array1);/*结果为$array2和$array1的值,键名被重新分配*/ 
print_r($result3); 
$result4 = array_merge($array1,$array2);/*结果为$array1和$array2的值,键名被重新分配*/ 
print_r($result4); 
?>
Copy after login

Output result:

Array 
( 
[1] => data 
) 
Array 
( 
[1] => 0 
) 
Array 
( 
[0] => data 
[1] => 0 
) 
Array 
( 
[0] => 0 
[1] => data 
)
Copy after login

Code:

<?php
$array1 = array(&#39;asd&#39;=>&#39;0&#39;); 
$array2 = array(&#39;asd&#39; => "data"); 
$result1 = $array2 + $array1;/*结果为$array2的值*/ 
print_r($result); 
$result = $array1 + $array2 ;/*结果为$array1的值*/ 
print_r($result); 
$result3 = array_merge($array2,$array1);/*结果为$array1*/ 
print_r($result3); 
$result4 = array_merge($array1,$array2);/*结果为$array2*/ 
print_r($result4);
?>
Copy after login

Output Result:

Array 
( 
[asd] => data 
) 
Array 
( 
[asd] => 0 
) 
Array 
( 
[asd] => 0 
) 
Array 
( 
[asd] => data 
)
Copy after login

1. The addition will prove that the natural index in the array will not be reset
2. In the addition method, the value in the added array will not be overwritten
3. The natural index in the merge function will be reset
4. The merge function does not matter the relationship between merge and merge. The value of the later array parameter will overwrite the value of the same key of the earlier array parameter

The above is the detailed content of Detailed example of the difference between php merging arrays using + operator and array function array_merge. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!