The difference between PHP function merging Array_merge and +

Source: Internet
Author: User

Array_merge () is a function in the PHP language that merges the cells of two or more arrays and appends the values in an array to the previous array. Returns an array as the result.

If the input array has the same string key name, the key's key value is the value corresponding to the last key name (the following value overrides the previous same value).

If the array is a numeric key name, the key name is re-indexed consecutively, that is, the subsequent value will not overwrite the original value, but is appended to the back.

Note: If you only enter an array into the Array_merge () function and the key name is an integer, the function returns a new array with an integer key name, with the key name re-indexed starting at 0. (see Example 2)

Note: When the key name in the subsequent array element is the same as the key name of the preceding array element, the key value of the corresponding key name in the result array is overwritten by the latter, which is the latter key value. (see example 3)

Grammar   

Array_merge (Array1[,array2[,array3 ...])
Parameters Describe
Array1 Necessary. The first array entered.
Array2 Necessary. The second array entered.
Array3 Optional. Multiple input arrays that can be specified.

Example 1:

<?php    $a 1=array ("a" = "Horse", "b" = "Dog");       $a 2=array ("c" = "Cow", "b" = "Cat");       Print_r (Array_merge ($a 1, $a 2)); ?>

Output:

Array ([A] = Horse [b] = Cat [c] = Cow)

Example 2:

Use only one array parameter:

$a =array (3=> "Horse",4=> "Dog"); Print_r (Array_merge ($a)); Output: Array ([0] = Horse [1] = Dog)

Example 3:

$a 1=array ("a" = "Horse", "b" = "Dog");   $a 2=array ("a" and "Cow", "b" = "Cat"); Print_r (Array_merge ($a 1, $a 2)); Output: Array ([a] = Cow [b] = Cat)

The "+" operator and Array_merge (): Array array_merge (array array1, array array2 [, array ...]) can combine multiple arrays, but there is a small difference in the use process.

The Array_merge is described in the reference manual 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 an array as the result.
If the input array has the same string key name, the value following the key name overrides the previous value. However, if the array contains numeric key names, subsequent values will not overwrite the original values, but are appended to the back.
The two differences are:
1. When the array key name is a numeric key name, the two arrays to be merged have the same name as the number key, using Array_merge () will not overwrite the original value, while using "+" to merge the array will return the first occurrence of the value as the final result, and the following array with the same key name of those values "discard" (note: Instead of overwriting, it retains the first value that appears).

Example:

    $array 1 = array (1=> ' 0 '); $array 2 = Array (1=> "data");    $result 1 = $array 2 + $array 1;/* result is the value of $array2   */Print_r ($result);   $result = $array 1 + $array 2;/* The result is the value of $array1 *   /Print_r ($result);   $result 3 = Array_merge ($array 2, $array 1);/* results are $array2 and $array1 values, key names are reassigned *   /Print_r ($result 3);   $result 4 = Array_merge ($array 1, $array 2);/* The result is a value of $array1 and $array2, the key name is reassigned *   /Print_r ($result 4); the output 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 the key name, but Array_merge () overrides the previous value of the same key name.
Example:

     $array 1 = Array (' asd ' = ' 0 '); $array 2 = Array (' ASD ' = ' data ');   $result 1 = $array 2 + $array 1;/* result is the value of $array2   */Print_r ($result);   $result = $array 1 + $array 2;/* The result is the value of $array1 *   /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);    The output is: Array ([ASD] = data) array ([ASD] + 0) Array ([ASD    ] +    0)    Array ([ASD] = Data)            

The difference between PHP function merging Array_merge and +

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.