PHP array merging: ["+" operator], [array_merge], and [array_merge_recursive]

Source: Internet
Author: User

1. "+" Operator
Rules:
When the key names of two arrays are numeric or string key names
$ C = $ A + $ B
Append the key name and value after $ A (the key name that $ B does not exist in $ ).
Note:
1. Do not overwrite, just append a key that does not exist and the corresponding value
2. Key names are not re-indexed
3. No matter whether it is all numeric keys or mixed keys, only the append key name and value are used. If the key name is the same, no append is made. That is, the first value is returned as the final result.
Example 1: Number key name

$a = array(    'a',);$b = array(    'u',);$c = $a + $b;var_dump($c);output:array(1) {  [0]=>  string(1) "a"}

Example 2: Number key name

$a = array(    66=>'a',);$b = array(    60=>'u',    66=>'c');$c = $a + $b;var_dump($c);output:array(2) {  [66]=>  string(1) "a"  [60]=>  string(1) "u"}

Example 3: character key name

<?php  $a = array(    1=>'a',    2=>'b',    'c'=>'c',    'd'=>'d',);$b = array(    1=>'u',    3=>'v',    'c'=>'w',    'd'=>'x',    'y'=>'y',    60=>'z',);$c = $a + $b;var_dump($c);?>output:array(7) {  [1]=>  string(1) "a"  [2]=>  string(1) "b"  ["c"]=>  string(1) "c"  ["d"]=>  string(1) "d"  [3]=>  string(1) "v"  ["y"]=>  string(1) "y"  [60]=>  string(1) "z"}

 
2. array array_merge (array array1 [, array array2 [, array...])
Rules:
Array_merge () combines the units of one or more arrays. The values in an array are appended to the values of the previous array. Returns an array of results.
If the input array contains the same string key name, the value after the key name overwrites the previous value.
However, if the array contains a number key name, the subsequent values will not overwrite the original values, but will be appended to the back.
If only one array is assigned and the array is indexed by number, the key name is re-indexed continuously.
Note:
1. Digital index, not covered. After the value is merged, the key name will be re-indexed continuously
2. String key name, the value after the key name will overwrite the previous value
Example 1: Number key name

$a = array(    'a',);$b = array(    'u',);$c = array_merge($a, $b);var_dump($c);output:array(2) {  [0]=>  string(1) "a"  [1]=>  string(1) "u"}

Example 2: Number key name

$a = array(    66=>'a',);$b = array(    60=>'u',    66=>'c');$c = array_merge($a, $b);var_dump($c);output:array(3) {  [0]=>  string(1) "a"  [1]=>  string(1) "u"  [2]=>  string(1) "c"}

Example 3: character key name

$a = array(    1=>'a',    2=>'b',    'c'=>'c',    'd'=>'d',);$b = array(    1=>'u',    3=>'v',    'c'=>'w',    'd'=>'x',    'y'=>'y',    60=>'z',);$c = array_merge($a, $b);var_dump($c);output:array(8) {  [0]=>  string(1) "a"  [1]=>  string(1) "b"  ["c"]=>  string(1) "w"  ["d"]=>  string(1) "x"  [2]=>  string(1) "u"  [3]=>  string(1) "v"  ["y"]=>  string(1) "y"  [4]=>  string(1) "z"}

 

3. array array_merge_recursive (array array1 [, array...])
Array_merge_recursive () combines the units of one or more arrays. The values of an array are appended to the values of the previous array. Returns an array of results.
If the input array has the same string key name, these values will be merged into an array, which will be recursive. Therefore, if a value is an array, this function combines the corresponding entries into another array.
However, if the array has the same Array key name, the latter value will not overwrite the original value, but will be appended to the back.
Note:
1. The rules are basically the same as those of array_merge, but when processing the same character key name, recursive append is used.
Example 1: Number key name

$a = array(    'a',);$b = array(    'u',);$c = array_merge_recursive($a, $b);var_dump($c);output:array(2) {  [0]=>  string(1) "a"  [1]=>  string(1) "u"}

Example 2: Number key name

$a = array(    'a',);$b = array(    'u',);$c = array_merge_recursive($a, $b);var_dump($c);output:array(2) {  [0]=>  string(1) "a"  [1]=>  string(1) "u"}

Example 3: character key name

$a = array(    1=>'a',    2=>'b',    'c'=>'c',    'd'=>'d',);$b = array(    1=>'u',    3=>'v',    'c'=>'w',    'd'=>'x',    'y'=>'y',    60=>'z',);$c = array_merge_recursive($a, $b);var_dump($c);output:array(8) {  [0]=>  string(1) "a"  [1]=>  string(1) "b"  ["c"]=>  array(2) {    [0]=>    string(1) "c"    [1]=>    string(1) "w"  }  ["d"]=>  array(2) {    [0]=>    string(1) "d"    [1]=>    string(1) "x"  }  [2]=>  string(1) "u"  [3]=>  string(1) "v"  ["y"]=>  string(1) "y"  [4]=>  string(1) "z"}

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.