Copyright © tutorialspoint.com

PHP Function array_merge_recursive()

previous
Advertisements

Syntax

array array_merge_recursive ( array $array1 [, array $array2...] )

Definition and Usage

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one.

If the input arrays have the same 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.

Paramters

ParameterDescription
array1Required.Specifies an array.
array2Optional.Specifies an array.

Return Values

It returns the resulting array.

Example

Try out following example:

<?php
$array1=array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
$array2=array("d"=>"Cow","a"=>"Cat","e"=>"elephant");

print_r(array_merge_recursive($array1,$array2));
?> 

This will produce following result:

Array 
( 
        [a] => Array ( [0] => Horse [1] => Cat ) 
        [b] => Cat 
        [c] => Dog 
        [d] => Cow 
        [e] => elephant 
)
previous

Copyright © tutorialspoint.com