PHP array_replace() function


The array_replace() function in PHP is used to replace the values of the first array with the values from subsequent arrays based on their keys. If a key from the first array exists in one of the subsequent arrays, the value from that subsequent array will overwrite the value in the first array.

Syntax:

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

Parameters:

  • $array: The input array whose values will be replaced.
  • $replacements: One or more arrays that provide the values to replace in the first array.

Return Value:

  • Returns an array with the values from the first array replaced by the values from the subsequent arrays. The keys from the first array are preserved.

Example 1: Basic Replacement

In this example, we demonstrate how to use array_replace() to replace specific values in an array.

<?php $array1 = ["a" => "apple", "b" => "banana", "c" => "cherry"]; $array2 = ["b" => "blueberry", "c" => "cranberry"]; $result = array_replace($array1, $array2); print_r($result); ?>

Output:

Array ( [a] => apple [b] => blueberry [c] => cranberry )

In this case, the values for keys b and c in $array1 are replaced with the corresponding values from $array2.

Example 2: Multiple Replacement Arrays

You can use array_replace() with multiple replacement arrays. The function processes the replacements from left to right.

<?php $array1 = ["a" => "apple", "b" => "banana", "c" => "cherry"]; $array2 = ["b" => "blueberry"]; $array3 = ["c" => "cranberry", "d" => "date"]; $result = array_replace($array1, $array2, $array3); print_r($result); ?>

Output:

Array ( [a] => apple [b] => blueberry [c] => cranberry [d] => date )

Here, the value for b is replaced by blueberry, the value for c is replaced by cranberry, and the new key d with the value date is added.

Example 3: Key Preservation

The keys from the original array are preserved even when replaced.

<?php $array1 = [0 => "zero", 1 => "one", 2 => "two"]; $array2 = [1 => "uno", 3 => "tres"]; $result = array_replace($array1, $array2); print_r($result); ?>

Output:

Array ( [0] => zero [1] => uno [2] => two [3] => tres )

In this example, the value for key 1 in $array1 is replaced by "uno" from $array2, and a new key 3 with the value "tres" is added.

Example 4: Handling Non-Existent Keys

If a key from the replacement arrays does not exist in the first array, it is simply added to the result.

<?php $array1 = ["x" => "X-ray", "y" => "Yankee"]; $array2 = ["z" => "Zulu"]; $result = array_replace($array1, $array2); print_r($result); ?>

Output:

Array ( [x] => X-ray [y] => Yankee [z] => Zulu )

Practical Usage:

  • array_replace() is useful for:
    • Merging configurations where default values may be overwritten by user-defined values.
    • Updating specific entries in a dataset while keeping others intact.
    • Constructing complex arrays from simpler parts while maintaining key-value associations.

Summary:

  • array_replace($array, $replacements...) replaces the values of the first array with those from subsequent arrays based on matching keys.
  • The function preserves the keys from the original array and returns a new array with the replacements applied.
  • It is a flexible tool for managing and updating arrays in PHP, particularly when dealing with associative arrays.