PHP Merge array array_merge function operator plus sign and the difference _php skill

Source: Internet
Author: User
Tags numeric
Array_merge in the reference manual are as follows:


array_merge () combines two or more arrays of cells, and the values in one array are appended to the previous array. Returns an array as the result.


If you enter an array that has the same string key name, the value that follows the key name overrides the previous value. However, if the array contains a numeric key name, the subsequent value will not overwrite the original value, but append to the back.

The difference between the two
is:


1. When the array key name is a numeric key name, when there is a number key in the two array to be merged with the same name, using Array_merge () does not overwrite the original value, and using the "+" merge array will return the first occurrence as the final result, while the following array has the same key name values " Discard the "Off" (note: not overwrite but retain the first occurrence of the value). Example:


Copy Code code as follows:

$array 1 = array (1=> ' 0 ');
$array 2 = Array (1=> "data");
$result 1 = $array 2 + $array 1;/* result is $array2 value * *
Print_r ($result);
$result = $array 1 + $array 2;/* Result $array1 Value * *
Print_r ($result);
$result 3 = Array_merge ($array 2, $array 1),/* results are $array2 and $array1 values, the key name is reassigned * *
Print_r ($result 3);
$result 4 = Array_merge ($array 1, $array 2),/* results are $array1 and $array2 values, the key name is reassigned * *
Print_r ($result 4);



output results are:


Copy Code code 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 is a number, but Array_merge () overrides the previous value of the same key name.


Example:


  
Copy Code code as follows:

$array 1 = Array (' ASD ' => ' 0 ');
$array 2 = Array (' ASD ' => "data");
$result 1 = $array 2 + $array 1;/* result is $array2 value * *
Print_r ($result);
$result = $array 1 + $array 2;/* Result $array1 Value * *
Print_r ($result);
$result 3 = Array_merge ($array 2, $array 1);/* The result is $array1*/
Print_r ($result 3);
$result 4 = Array_merge ($array 1, $array 2);/* The result is $array2*/
Print_r ($result 4);



output results are:


Copy Code code as follows:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.