Similarities and Differences in Three PHP Methods of Merging Arrays_PHP Tutorial

WBOY
Release: 2016-07-13 10:33:14
Original
862 people have browsed it

1. "+" operator

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:

  1. does not overwrite, just appends the non-existing key name and corresponding value.
  2. Key names are not re-indexed.
  3. Whether it is all numeric key names or mixed, only the key name and value are appended. If the key names are the same, no appending is performed, that is, the first appearing value is returned as the final result.
<?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" }
?>
Copy after login

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" }
?>
Copy after login

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" }
?>
Copy after login

2. array array_merge ( array array1 [, array array2 [, array ...]] )

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:

  1. Numeric index will not be overwritten. After the values ​​are merged, the key names will be re-indexed in a continuous manner
  2. String key name, the value after the key name will overwrite the previous value
<?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" }
?>
Copy after login

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" }
?>
Copy after login

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" }
?>
Copy after login

3. array array_merge_recursive ( array array1 [, array ...] )

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" }
?>
Copy after login

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" }
?>
Copy after login

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" }
?>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752522.htmlTechArticle1. "+" operator rule: When the key names of the two arrays are numeric key names or string keys The name can be directly +, $c = $a + $b, append (the key name of $b does not exist in $a) key name and value after $a. ...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!