English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP array_merge_recursive() Function Usage and Example

PHP Array Function Manual

The PHP array_merge_recursive() function recursively merges one or more arrays

Syntax

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

Definition and Usage

 The array_merge_recursive() function merges the elements of one or more arrays together, appending the values of one array to the end of the previous array. It returns the resulting array.
If the input array contains the same string key name, these values will be merged into an array, which will be recursive. Therefore, if a value itself is an array, this function will merge it into another array according to the corresponding entry. It should be noted that if the array has the same numeric key name, the latter value will not overwrite the original value but will be appended to the end.

Parameter

Serial NumberParameters and Description
1

array1(Required)

It specifies an array.

2

array2(Optional)

It specifies an array.

Return Value

It returns the resulting array.

Online Example

It merges the elements of two arrays together and appends the values of one array to the end of the previous array.

<?php
   $input1 = array("a" => "Horse", "b" => "Cat", "c" => "Dog");
   $input2 = array("d" => "Cow", "a" => "Cat", "e" => "elephant");
   
   print_r(array_merge_recursive($input1$input2));
?>
Test and See‹/›

Output Result:

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

PHP Array Function Manual