


The difference between PHP merge array array_merge function operator plus sign and - PHP tutorial
Jul 21, 2016 pm 03:49 PM
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:
$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:
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:
');
$result4 = array_merge($array1,$array2);/*The result is $array2*/
print_r($result4);
The output result is:
Copy code
The code is as follows:
Array
Array
(
[asd] => ; 0
)
Array
(
[asd] => data
)
http://www.bkjia.com/PHPjc/319608.html
www.bkjia.com
http: //www.bkjia.com/PHPjc/319608.html

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

Does Bitcoin have stocks? Does Bitcoin have equity?

deepseek What is the difference between r1 and v3 version

How To Set Up Visual Studio Code (VS Code) for PHP Development

How do you parse and process HTML/XML in PHP?

PHP Program to Count Vowels in a String

What is the difference between pre-market and after-market trading? Detailed explanation of the differences between pre-market and after-market trading
