phpday 2027 - Call for Papers

array_replace_recursive

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

array_replace_recursive — Sostituisce ricorsivamente nel primo array gli elementi provenienti dagli array passati

Descrizione

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

array_replace_recursive() sostituisce i valori di array con gli stessi valori da tutti gli array seguenti. Se una chiave del primo array esiste nel secondo array, il suo valore verrà sostituito dal valore del secondo array. Se la chiave esiste nel secondo array e non nel primo, la chiave verrà creata nel primo array. Se una chiave esiste solo nel primo array, verrà lasciata così com'è. Se vengono passati più array per la sostituzione, verranno elaborati in ordine, e l'array successivo sovrascriverà i valori precedenti.

array_replace_recursive() è ricorsiva: effettuerà la ricorsione negli array e applicherà lo stesso processo ai valori interni.

Quando il valore nel primo array è scalare, verrà sostituito dal valore del secondo array, che sia scalare o array. Quando il valore nel primo array e nel secondo array sono entrambi array, array_replace_recursive() sostituirà il loro rispettivo valore in modo ricorsivo.

Elenco dei parametri

array

L'array nel quale gli elementi sono sostituiti.

replacements

Gli array da cui verranno estratti gli elementi.

Valori restituiti

Restituisce un array.

Esempi

Example #1 Esempio di array_replace_recursive()

<?php
$base = array('citrus' => array( "orange") , 'berries' => array("blackberry", "raspberry"), );
$replacements = array('citrus' => array('pineapple'), 'berries' => array('blueberry'));

$basket = array_replace_recursive($base, $replacements);
print_r($basket);

$basket = array_replace($base, $replacements);
print_r($basket);
?>

Il precedente esempio visualizzerà:

Array
(
    [citrus] => Array
        (
            [0] => pineapple
        )

    [berries] => Array
        (
            [0] => blueberry
            [1] => raspberry
        )

)
Array
(
    [citrus] => Array
        (
            [0] => pineapple
        )

    [berries] => Array
        (
            [0] => blueberry
        )

)

Example #2 array_replace_recursive() ed il comportamento ricorsivo

<?php
$base = array('citrus' => array("orange") , 'berries' => array("blackberry", "raspberry"), 'others' => 'banana' );
$replacements = array('citrus' => 'pineapple', 'berries' => array('blueberry'), 'others' => array('litchis'));
$replacements2 = array('citrus' => array('pineapple'), 'berries' => array('blueberry'), 'others' => 'litchis');

$basket = array_replace_recursive($base, $replacements, $replacements2);
print_r($basket);

?>

Il precedente esempio visualizzerà:

Array
(
    [citrus] => Array
        (
            [0] => pineapple
        )

    [berries] => Array
        (
            [0] => blueberry
            [1] => raspberry
        )

    [others] => litchis
)

Vedere anche:

User Contributed Notes 1 note

up
10
Pau Prat Torrella
6 years ago
Note that function will NOT replace a sub-tree from you $array1 if its value in $array2 is an empty array. 
Even tho the key for this dimension is technically 'set'. 

(I suppose it treats it as just another recursive level to dive in, finding no key to compare, backtracking while leaving this sub-tree alone)

For example:

$array1  = ['first' => ['second' => 'hello']];
$array2  = ['first' => []];              
$result = array_replace_recursive($array1, $array2);

$result is still: ['first' => ['second' => 'hello']].
Adding new notes is disabled. Please see the links in the Found A Problem? section.
To Top