w3resource

PHP: array_merge_ recursive() function

PHP: Merge two or more arrays recursively

The array_merge_recursive() function is used to merge the elements of one or more arrays together. The elements of one are appended to the end of the previous one.

If the input arrays have matching string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too.

If the input arrays contain numeric keys, the later value will be appended instead of overriding the original value.

Version:

(PHP 4 and above)

Syntax:

array_merge_recursive(array_name1, array_name2...) 

Parameters:

Name Description Required /
Optional
Type
array_name1 Specifies the name of the array. Required Array
array_name2.. Specifies the name of the array. Optional Array

Return value:

The merged array.

Value Type: Array

Example:

<?php
$array1=array("Subject" => "Physics","Chemistry", "Biology");
$array2=array("Class-XI", "Class-XII", "Roll"=>20);
$result=array_merge_recursive($array1, $array2);
print_r($result);
?>

Output:

Array ( [Subject] => Physics [0] => Chemistry [1] => Biology [2] => Class-XI [3] => Class-XII [Roll] => 20 ) 

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous: array_map
Next: array_merge



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/php/function-reference/array_merge_recursive.php