Home Backend Development PHP Tutorial The difference between PHP merge array array_merge function operator plus sign and - PHP tutorial

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

Jul 21, 2016 pm 03:49 PM
array merge php and function plus the difference merge exist manual array of operator

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] =&gt ; 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...
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

Does Bitcoin have stocks? Does Bitcoin have equity? Does Bitcoin have stocks? Does Bitcoin have equity? Mar 03, 2025 pm 06:42 PM

Does Bitcoin have stocks? Does Bitcoin have equity?

deepseek What is the difference between r1 and v3 version deepseek What is the difference between r1 and v3 version Feb 19, 2025 pm 03:24 PM

deepseek What is the difference between r1 and v3 version

Summary of FAQs for DeepSeek usage Summary of FAQs for DeepSeek usage Feb 19, 2025 pm 03:45 PM

Summary of FAQs for DeepSeek usage

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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 What is the difference between pre-market and after-market trading? Detailed explanation of the differences between pre-market and after-market trading Mar 03, 2025 pm 11:54 PM

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

See all articles