Definition
The array_replace() function replaces the values in an array with the values from other arrays.
Syntax
array_replace(array1, array2, array3, ...)
Parameters
Parameter | Description |
---|---|
array1 |
Required. Specifies an array. |
array2 |
Optional. Specifies an array which will replace the values of array1. |
array3... |
Optional. Specifies more arrays to replace the values of array1 and array2, etc. Values from later arrays will overwrite the previous ones. |
Example
<?php
// Example 1
$cities = array("New York", "Salt Lake", "Tokyo");
$cities2 = array("London", "Berlin", "Cairo");
print_r(array_replace($cities, $cities2));
echo "<br>";
// Example 2
$ages = array("Mark" => 22, "Jeff" => 32, "Mike" => 28);
$ages2 = array("Mark" => 23, "Jeff" => 21, "Mike" => 18);
print_r(array_replace($ages, $ages2));
echo "<br>";
// Example 3
$ages = array("Mark" => 22, "Jeff" => 32, "Mike" => 28);
$ages2 = array("Alex "=> 23, "Rey" => 21, "Ivan" => 18);
print_r(array_replace($ages, $ages2));
echo "<br>";