Home > Backend Development > PHP Tutorial > 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

黄舟
Release: 2023-03-17 08:04:02
Original
2336 people have browsed it

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));
?>
Copy after login

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...)
Copy after login
ParametersDescription
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));
?>
Copy after login

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)
Copy after login


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!

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