PHP array merging: Analysis of the differences between + operator, array_merge, and array_merge_recursive

WBOY
Release: 2016-07-25 08:58:53
Original
1269 people have browsed it
  1. $a = array(
  2. 'a',
  3. );
  4. $b = array(
  5. 'u',
  6. );
  7. $c = $a + $b;
  8. var_dump($c);
Copy code

Output:

  1. $a = array(
  2. 66=>'a',
  3. );
  4. $b = array(
  5. 60=>'u',
  6. 66=>'c'
  7. );
  8. $c = $a + $b;
  9. var_dump($c);
Copy code

Output:

  1. $a = array(
  2. 1=>'a',
  3. 2=>'b',
  4. 'c'=>'c',
  5. 'd'= >'d',
  6. );
  7. $b = array(
  8. 1=>'u',
  9. 3=>'v',
  10. 'c'=>'w',
  11. 'd'=> ;'x',
  12. 'y'=>'y',
  13. 60=>'z',
  14. );
  15. $c = $a + $b;
  16. var_dump($c);
  17. ?>
Copy code

Output:

  1. $a = array(
  2. 'a',
  3. );
  4. $b = array(
  5. 'u',
  6. );
  7. $c = array_merge($a, $b);
  8. var_dump($ c);
Copy code

Output:

  1. $a = array(
  2. 66=>'a',
  3. );
  4. $b = array(
  5. 60=>'u',
  6. 66=>'c'
  7. );
  8. $c = array_merge($a, $b);
  9. var_dump($c);
Copy code

Output:

  1. $a = array(
  2. 1=>'a',
  3. 2=>'b',
  4. 'c'=>'c',
  5. 'd'=>'d' ,
  6. );
  7. $b = array(
  8. 1=>'u',
  9. 3=>'v',
  10. 'c'=>'w',
  11. 'd'=>'x',
  12. 'y'=>'y',
  13. 60=>'z',
  14. );
  15. $c = array_merge($a, $b);
  16. var_dump($c);
Copy code

Output:

  1. $a = array(
  2. 'a',
  3. );
  4. $b = array(
  5. 'u',
  6. );
  7. $c = array_merge_recursive($a, $b);
  8. var_dump($ c);
Copy code

Output:

  1. $a = array(
  2. 'a',
  3. );
  4. $b = array(
  5. 'u',
  6. );
  7. $c = array_merge_recursive($a, $b);
  8. var_dump($ c);
Copy code

Output:

  1. $a = array(
  2. 1=>'a',
  3. 2=>'b',
  4. 'c'=>'c',
  5. 'd'=>'d' ,
  6. );
  7. $b = array(
  8. 1=>'u',
  9. 3=>'v',
  10. 'c'=>'w',
  11. 'd'=>'x',
  12. 'y'=>'y',
  13. 60=>'z',
  14. );
  15. $c = array_merge_recursive($a, $b);
  16. var_dump($c);
Copy code

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" }


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
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!