PHP array_replace Function
last modified March 13, 2025
The PHP array_replace
function replaces elements from passed arrays
into the first array. It works with both numeric and string keys.
Basic Definition
The array_replace
function replaces values in the first array with
values from following arrays. If a key exists in multiple arrays, the last
array's value is used.
Syntax: array_replace(array $array, array ...$replacements): array
.
The function returns the modified array. Original arrays are not changed.
Basic array_replace Example
This shows simple replacement of values in an associative array.
<?php $base = ["a" => 1, "b" => 2, "c" => 3]; $replace = ["b" => 20, "d" => 40]; $result = array_replace($base, $replace); print_r($result);
The value for key "b" is replaced with 20, and a new key "d" is added. Original keys "a" and "c" remain unchanged in the resulting array.
Multiple Array Replacement
Demonstrates how values are replaced when multiple arrays are provided.
<?php $base = ["apple" => "red", "banana" => "yellow"]; $replace1 = ["apple" => "green", "orange" => "orange"]; $replace2 = ["banana" => "brown"]; $result = array_replace($base, $replace1, $replace2); print_r($result);
The value for "apple" comes from $replace1, "banana" from $replace2, and "orange" is added. Later arrays take precedence over earlier ones.
Numeric Key Replacement
Shows how numeric keys are handled by the array_replace function.
<?php $base = [10 => "ten", 20 => "twenty"]; $replace = [10 => "TEN", 30 => "THIRTY"]; $result = array_replace($base, $replace); print_r($result);
Numeric key 10 is replaced, key 20 remains, and key 30 is added. The function works the same way with numeric and string keys.
Empty Arrays Behavior
Demonstrates what happens when empty arrays are passed to array_replace.
<?php $base = ["a" => 1, "b" => 2]; $empty1 = []; $empty2 = []; $result = array_replace($base, $empty1, $empty2); print_r($result);
Empty arrays don't affect the result. The output is identical to the base array since no replacements were specified in the empty arrays.
Combining Numeric and Associative
Shows how array_replace handles mixed numeric and associative arrays.
<?php $base = [0 => "zero", "color" => "red"]; $replace = [0 => "ZERO", 1 => "one", "color" => "blue"]; $result = array_replace($base, $replace); print_r($result);
Numeric key 0 is replaced, string key "color" is updated, and new key 1 is added. The function handles mixed key types seamlessly.
Best Practices
- Key Clarity: Be mindful of key collisions between arrays.
- Order Matters: Later arrays override earlier ones.
- Immutability: Original arrays remain unchanged.
- Performance: For large arrays, consider memory usage.
Source
PHP array_replace Documentation
This tutorial covered the PHP array_replace
function with practical
examples showing its usage for array manipulation scenarios.
Author
List all PHP Array Functions.