Jump to: navigation, search

Php array replace recursive

From w3cyberlearnings

Contents

PHP function array_replace_recursive

This function manipulates an array by replacing the replacement array into the base array recursively.

Syntax array_replace_recursive

  • base: origional array
  • replacement: replacement array
array_replace_recursive(base, replacement);

Note

This function is for PHP 5 >= 5.3.0.

Example 1


<?php

$base = array('dog'=>array('lolo',29),'cat'=>array(29));

$replacements = array('dog'=>'dodo','cat'=>array('mark',13));

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

?>

Output

Array
(
    [dog] => dodo
    [cat] => Array
        (
            [0] => mark
            [1] => 13
        )

)


Example 2

<?php

$base = array(3=>'bob',4=>'john');

$replacements = array(4=>'dodo',3=>array('mark',13));

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

?>

Output


Array
(
    [3] => Array
        (
            [0] => mark
            [1] => 13
        )

    [4] => dodo
)

Related Links


Navigation
Web
SQL
MISC
References