array_replace()
Published Sep 1, 2023
Contribute to Docs
The array_replace()
function in PHP is used to replace the values of an array with values from one or more arrays.
Syntax
array_replace(array $array, array ...$replacements)
$array
(array): The original array that will be used as the base. The original array will not be modified, a new array will be returned with the associated replacement values.$replacements
(array): One or more arrays containing the replacement values.
Note: If multiple arrays contain the same key, the value from the last array in the argument list will overwrite the previous ones.
Example
The code below demonstrates the basic use of the array_replace()
function:
<?php$array1 = ['a' => 1, 'b' => 2, 'c' => 3];$array2 = ['b' => 10, 'c' => 20, 'd' => 30];$replacedArray = array_replace($array1, $array2);print_r($replacedArray);?>
This will return the following output:
Array
(
[a] => 1
[b] => 10
[c] => 20
[d] => 30
)
Codebyte Example
The following code is runnable and demonstrates another implementation of the array_replace()
function:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.