Func Array Replace
# PHP array_replace() Function
[Complete PHP Array Reference](#)
## Example
Replace the values of the first array ($a1) with the values from the second array ($a2):
[Run Example Β»](#)
* * *
## Definition and Usage
The array_replace() function replaces the values of the first array with values from subsequent arrays.
**Tip:** You can pass one array, or multiple arrays to the function.
If a key exists in the first array array1 and also in the second array array2, the value in the first array array1 will be replaced by the value in the second array array2. If a key exists only in the first array array1, it will remain unchanged. (See Example 1 below)
If a key exists in the second array array2, but not in the first array array1, this element will be created in the first array array1. (See Example 2 below)
If multiple replacement arrays are passed, they will be processed in order, with the values from later arrays overwriting the values from earlier arrays. (See Example 3 below)
**Tip:** Use [array_replace_recursive()](#) to replace values recursively.
* * *
## Syntax
array_replace(_array1,array2,array3..._)
| Parameter | Description |
| --- | --- |
| _array1_ | Required. Specifies an array. |
| _array2_ | Optional. Specifies an array whose values will replace the values in _array1_. |
| _array3,..._ | Optional. Specifies additional arrays whose values will replace the values in _array1_ and _array2, ..._. The values from later arrays overwrite the values from earlier arrays. |
## Technical Details
| Return Value: | Returns the replaced array, or NULL on error. |
| --- |
| PHP Version: | 5.3.0+ |
* * *
## More Examples
## Example 1
If a key exists in the first array array1 and also in the second array array2, the value in the first array array1 will be replaced by the value in the second array array2. If a key exists only in the first array array1, it will remain unchanged.
"red","b"=>"green");
$a2=array("a"=>"orange","burgundy");
print_r(array_replace($a1,$a2));
?>
[Run Example Β»](#)
## Example 2
If a key exists in the second array array2, but not in the first array array1, this element will be created in the first array array1.
"red","green");
$a2=array("a"=>"orange","b"=>"burgundy");
print_r(array_replace($a1,$a2));
?>
[Run Example Β»](#)
## Example 3
Using three arrays - the last array ($a3) will overwrite the previous arrays ($a1 and $a2):
[Run Example Β»](#)
## Example 4
Using numeric keys - If a key exists in the second array array2, but not in the first array array1, this element will be created in the first array array1:
"orange",3=>"burgundy");
print_r(array_replace($a1,$a2));
?>
[Run Example Β»](#)
* * Complete PHP Array Reference](#)
YouTip