PHP array_replace() Function
The array_replace() function replaces the values of the first array with the values from following arrays.
Tip: You can assign one array to the function, or as many as you like.
If a key from array1 exists in array2, values from array1 will be replaced by the values from array2. If the key only exists in array1.
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. |
Syntax
array_replace(array1,array2,array3...)
Example
$array1 = array("a"=>"Alex","b"=>"James");
$array2 = array("a"=>"Samuel","Cooper");
print_r(array_replace($array1,$array2));
Output
Array ( [a] => Samuel [b] => James [0] => Cooper )
More Example
$array1 = array("Alex","Cooper");
$array2 = array("Cooper","Lachlan");
print_r(array_replace($array1,$array2));
Output
Array ( [0] => Cooper [1] => Lachlan )
Leave a Reply