PHP array_replace_recursive function

Last Updated: February 14, 2022

array_replace_recursive – Replaces elements from passed arrays into the first array recursively the array and reduce it to a single value using a callback function


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

Supports (PHP 4, PHP 5, PHP 7, PHP 8)

If a key from the first array exists in the second array, its value will be replaced by the value from the second array. If the key exists in the second array, and not the first, it will be created in the first array. If a key only exists in the first array, it will be left as is. If several arrays are passed for replacement, they will be processed in order, the later array overwriting the previous values.


Parameter Description
array Required. target array
replacements Required. Arrays from which elements will be extracted. 

Return Value

returns the resulting value


$base = array('days' => array( "Sun") , 'months' => array("Jan", "Feb"), );
$replacements = array('days' => array('Sunday'), 'months' => array('January'));
$basket = array_replace_recursive($base, $replacements);


Array ( [days] => Array ( [0] => Sunday ) [months] => Array ( [0] => January [1] => Feb ) )