PHP array_replace() Function
Last Updated : 17 Mar 2025
The array_replace() function is a built-in function in PHP. The PHP array_replace( ) function is used to replace the values of the first array with the values from following arrays. This function was introduced in PHP 5.3.0.
Syntax
Parameter
| Parameter | Description | Is compulsory |
|---|---|---|
| array1 | Specifies an array. | compulsory |
| array2 | Specifies an array which will replace the values of array1. | Optional |
| array3,... | Specifies more arrays to replace the values of array1 and array2, etc. Values from later arrays will overwrite the previous ones. | Optional |
Return type
The array_replace( ) function return the replaced array. It will return null if an error occurs.
Example 1
Output:
Array
(
[a] =>Pataliputra
[b] =>chennai
[0] => Noida
)
Example 2
Output:
Array
(
[a] =>javatpoint
[0] => java
[b] => PHP
)
Example 3
Output:
Array
(
[0] =>arjun
[1] =>sehwag
[2] =>kapil
[3] =>rahul
[4] =>klrahul
)
Example 4
Output:
Array
(
[j] =>javatpoint
[p] =>php
)
Next TopicPhp-array-replace-recursive-function