Home  >  Article  >  Backend Development  >  PHP function array_merge_recursive() that merges one or more arrays into one array

PHP function array_merge_recursive() that merges one or more arrays into one array

黄舟
黄舟Original
2017-11-08 09:26:162293browse

Example

Merge two arrays into one array:

<?php
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge_recursive($a1,$a2));
?>

Definition and usage

array_merge_recursive() function is used to merge one or more arrays into one array.

The difference between this function and the array_merge() function is that it handles the situation where two or more array elements have the same key name. array_merge_recursive() will not perform key name overwriting, but will recursively form multiple values ​​with the same key name into an array.

Note: If you only input an array to the array_merge_recursive() function, the result is the same as array_merge(), the function will return a new array with integer keys whose keys begin with 0 Begin reindexing.

Syntax

array_merge_recursive(array1,array2,array3...)
Parameters Description
array1 Required. Specifies an array.
array2 Optional. Specifies an array.
array3 Optional. Specifies an array.

Technical details

Return value: Returns the merged array.
PHP version: 4.0.1+

[Example]

<?php 
$arr1 = array("color"=>array("favorite"=>"red"),5); 
$arr2 = array(10,"color"=>array("favorite"=>"green","blue")); 
var_dump(array_merge_recursive($arr1,$arr2));
?>

Output:

array(3) {
  ["color"]=>
  array(2) {
    ["favorite"]=>
    array(2) {
      [0]=>
      string(3) "red"
      [1]=>
      string(5) "green"
    }
    [0]=>
    string(4) "blue"
  }
  [0]=>
  int(5)
  [1]=>
  int(10)


The above is the detailed content of PHP function array_merge_recursive() that merges one or more arrays into one array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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