Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

array_merge_recursive

Description

The array_merge_recursive of Array for PHP merge one or more arrays recursively.

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. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.

Syntax

array_merge_recursive(
    array ...$arrays
): array

Parameters

arrays

Variable list of arrays to recursively merge.

Return

An array of values resulted from merging the arguments together. If called without any arguments, returns an empty array.

Examples

1 · void

<?

$return = array_merge_recursive();

print_r($return);

?>
Array
(
)

2 · arrays · one · indexed

<?

$arrays = array(0, 1);

$return = array_merge_recursive($arrays);

print_r($return);

?>
Array
(
    [0] => 0
    [1] => 1
)

3 · arrays · one · associative

<?

$arrays = array("a" => 0, "b" => 1);

$return = array_merge_recursive($arrays);

print_r($return);

?>
Array
(
    [a] => 0
    [b] => 1
)

4 · arrays · multiple · indexed

<?

$arrays1 = array(0, 1);
$arrays2 = array(2, 3);

$return = array_merge_recursive($arrays1, $arrays2);

print_r($return);

?>
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
)

5 · arrays · multiple · associative

<?

$arrays1 = array("a" => 0, "b" => 1);
$arrays2 = array("c" => 2, "d" => 3);

$return = array_merge_recursive($arrays1, $arrays2);

print_r($return);

?>
Array
(
    [a] => 0
    [b] => 1
    [c] => 2
    [d] => 3
)

6 · same key · indexed

<?

$arrays1 = array(0, 1);
$arrays2 = array(0 => 2, 1 => 3);

$return = array_merge_recursive($arrays1, $arrays2);

print_r($return);

?>
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
)

7 · same key · associative

<?

$arrays1 = array("a" => 0, "b" => 1);
$arrays2 = array("a" => 2, "b" => 3);

$return = array_merge_recursive($arrays1, $arrays2);

print_r($return);

?>
Array
(
    [a] => Array
        (
            [0] => 0
            [1] => 2
        )

    [b] => Array
        (
            [0] => 1
            [1] => 3
        )

)
HomeMenu