PHP array_replace function

Last Updated: February 14, 2022

array_replace — Replaces elements from passed arrays into the first array

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 key exists in second array not in first array they it is added to the first array.

Syntax

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

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

Parameters

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

Return Value

returns the resulting value

Example

<?php
$base = array('days' => array( "Sun") , 'months' => array("Jan", "Feb"), );
$replacements = array('days' => array('Sunday'), 'months' => array('January'));
$basket = array_replace($base, $replacements);
print_r($basket);
?>

Output

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