PHP array Merge functions (+), Array_merge, array_merge_recursive

Source: Internet
Author: User
Tags arrays numeric

In PHP, there are 3 methods for array merging: operator (+), Array_merge, Array_merge_recursive, where the first two methods are the merging of one-dimensional arrays, and the third is recursive merging.
Before you say that the array of PHP is merged, understand that the PHP array is divided into indexed arrays and associative arrays. An indexed array is an array that has no key value and a numeric key value, and an associative array is an array of key values as characters.

Here are the differences between the operator (+) and Array_merge:

1. When the array is indexed, merges an array of the same key values, with the operator taking the first value as the merged result, Array_merge merging the array of the same key value and indexing (the numeric index is also rebuilt from 0 when the array of different key values is merged).

1, "+" operator
Rules:
When the key name of a two array is a numeric key name or a string key name
$c = $a + $b
Append ($b Key name that does not exist in $a) after $a key name and value
Attention:
1, do not overwrite, just append nonexistent key name and corresponding value
2, the key name does not re-index
3, whether it is all numeric keys or mixed, just append key names and values, if the same key name is not to append, that is, the first occurrence of the value as the final result returned
Example 1: Numeric Key Name

The code is as follows Copy Code

$a = Array (
' A ',
);
$b = Array (
' U ',
);
$c = $a + $b;
Var_dump ($c);

Output
Array (1) {
[0]=>
String (1) "a"
}
Example 2: Numeric 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 keys 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. When an associative array is merged, an array of the same character keys values is combined, and the operation conforms to the rule followed by the preceding overlay, and the Array_merge is preceded by the overlay.

Cases:

The code is as follows Copy Code

$arr 5 = Array (' A ' => ' aaaa ');
$arr 6 = Array (' A ' => ' bbbb ');

Var_dump ($arr 5+ $arr 6);
Var_dump (Array_merge ($arr 5, $arr 6));
Output:
Array (size=1)
' A ' => string ' aaaa ' (length=4)
Array (size=1)
' A ' => string ' bbbb ' (length=4)


Array_merge () merges one or more PHP 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 a numeric key name, the subsequent value will not overwrite the original value, but append to the back.
If only one array is given and the array is a numeric index, the key name is sequentially indexed.

The 3.array_merge_recursive consolidation rules are the same as Array_merge, except that the array_merge_recursive supports merging multidimensional arrays.

Cases:

The code is as follows Copy Code

$a = Array (

1=> ' A ',
2=> ' B ',
' C ' => ' C ',
' d ' => ' d ',

);

$b = Array (
1=> ' U ',
3=> ' V ',
' C ' => ' W ',
' d ' => ' X ',
' Y ' => ' y ',
60=> ' Z ',

);

Var_dump (array_merge_recursive ($a, $b));

Output:

Array (size=8)
  0 => string ' a ' (length=1)
  1 => string ' B ' (length=1)
  ' C ' => br>     Array (size=2)
      0 => string ' C ' (length=1)
  & nbsp;   1 => string ' W ' (length=1)
  ' d ' =>
    Array (size=2)
 &nbs p;    0 => string ' d ' (length=1)
      1 => string ' x ' (length=1)
&N Bsp 2 => string ' U ' (length=1)
  3 => string ' V ' (length=1)
  ' y ' => string ' y ' (length=1)
&nbs P 4 => string ' Z ' (length=1)

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.