Home >Backend Development >PHP Tutorial >Similarities and Differences in Three PHP Methods of Merging Arrays_PHP Tutorial
Rule: When the key names of the two arrays are numeric key names or string key names, you can directly +, $c = $a + $b, append after $a ($b is a key that does not exist in $a name) key name and value.
Note:
<?php $fruit_1 = array( 'apple', 'banana' ); $fruit_2 = array( 'orange', 'lemon' ); $fruit = $fruit_1 + $fruit_2; var_dump($fruit); // output: // array(2) { [0]=> string(5) "apple" [1]=> string(6) "banana" } ?>
Number key name:
<?php $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" } ?>
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" } ?>
Rule: array_merge() merges the cells of one or more arrays, and the values in one array are appended to the previous array. Returns the resulting array. If the input array has the same string key name, 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. If only an array is given and the array is numerically indexed, the keys are re-indexed consecutively.
Note:
<?php $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" } ?>
Number key name:
<?php $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" } ?>
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 = 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" } ?>
array_merge_recursive() Merges the cells of one or more arrays, with the values in one array appended to the previous array. Returns the resulting array.
If the input arrays have the same string key name, the values will be merged into an array, which will go on recursively, so if a value itself is an array, this function will put it according to the corresponding entry. It is merged into another array.
However, if the arrays have the same array key name, the latter value will not overwrite the original value, but will be appended to it.
Note: The rules are basically the same as array_merge, except that recursive append is used when processing the same character key name.
<?php $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" } ?>
Number key name:
<?php $a = array( 66=>'a' ); $b = array( 60=>'u', 66=>'c' ); $c = array_merge_recursive($a, $b); var_dump($c); // output: // array(3) { [0]=> string(1) "a" [1]=> string(1) "u" [2]=> string(1) "c" } ?>
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 = 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" } ?>