The array_replace Function
The array_replace
function replaces values
of the first array with values from the same keys
in other passed arrays. If a key
from the first array is present in the second
array, its value is replaced by the value
from the second array. If a key exists in the second
array but is absent in the first - it will be
created in the first array. If a key is present
only in the first array, it remains unchanged.
If several arrays are passed for replacement, they will be processed in the order of passing and later arrays will overwrite values from the previous ones.
Syntax
array_replace(array $array, array ...$replacements): array
Example
Let's perform the described replacement:
<?php
$arr1 = ['a' => 1, 'b'=> 2, 'c' => 3];
$arr2 = ['a' => '!', 'c' => '?'];
$res = array_replace($arr1, $arr2);
var_dump($res);
?>
Code execution result:
['a' => '!', 'b'=> 2, 'c' => '?']
See Also
-
the
str_replace
function,
which replaces string characters