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

array_replace_recursive

Description

The array_replace_recursive of Array for PHP replaces elements from passed arrays into the first array recursively.

Syntax

array_replace_recursive(
    array $array,
    array ...$replacements
): array

Parameters

array

The array in which elements are replaced.

replacements

Arrays from which elements will be extracted.

Return

Returns an array.

Examples

1 · array

<?

$array =
[
    0,
    1,
    2,
    [
        0,
        1,
        2,
        [
            0,
            1,
            2
        ]
    ]
];

$return = array_replace_recursive($array);

print_r($return);
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => Array
        (
            [0] => 0
            [1] => 1
            [2] => 2
            [3] => Array
                (
                    [0] => 0
                    [1] => 1
                    [2] => 2
                )

        )

)

2 · replacements

<?

$array =
[
    0,
    1,
    2,
    [
        0,
        1,
        2,
        [
            0,
            1,
            2
        ]
    ]
];
$replacements =
[
    "replace1",
    [
        "replace2",
        "replace3"
    ]
];

$return = array_replace_recursive($array, $replacements);

print_r($return);
Array
(
    [0] => replace1
    [1] => Array
        (
            [0] => replace2
            [1] => replace3
        )

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

        )

)

3 · replacements · key

<?

$array =
[
    0,
    1,
    2,
    [
        0,
        1,
        2,
        [
            0,
            1,
            2
        ]
    ]
];
$replacements =
[
    1 => "replace1",
    3 =>
    [
        "replace2",
        "replace3"
    ]
];

$return = array_replace_recursive($array, $replacements);

print_r($return);
Array
(
    [0] => 0
    [1] => replace1
    [2] => 2
    [3] => Array
        (
            [0] => replace2
            [1] => replace3
            [2] => 2
            [3] => Array
                (
                    [0] => 0
                    [1] => 1
                    [2] => 2
                )

        )

)