PHP array_replace() Function
Last Updated :
20 Jun, 2023
The array_replace() function is a builtin function in PHP and it takes a list of arrays separated by commas (,) as parameters and replaces all those values of the first array that have same keys in the other arrays. The replacement is done according to the following rules:
- If a key in the first array also exists in the second array, then its value in the first array is replaced with the value of that key in the second array.
- If a key in the second array does not exist in the first array, then it is created in the first array and its value from the second array is copied in the first array.
- If a key in the first array is not present in any of the subsequent arrays, then the value of that key is left unchanged in the first array.
- The arrays are processed in order in which they are passed to the function, therefore if a key of the first array is present in more than one array, then its value will be replaced by the value of the array in which it occurs the last time.
Syntax:
array array_replace ( $array1, $array2, ...., $arrayn )
Parameters: This function accepts a list of arrays as parameters. The first parameter to the function is the array that is to be replaced. The rest of the parameters to the function are the arrays whose value is to be copied into the first array.
Return value: This function returns an array formed after modifying the first array in parameters.
Examples:
Input : $array1 = array("orange", "banana", "apple", "raspberry")
$array2 = array(0 => "pineapple", 4 => "cherry")
$array3 = array(0 => "grape")
array_replace($array1, $array2, $array3)
Output : Array
(
[0] => grape
[1] => banana
[2] => apple
[3] => raspberry
[4] => cherry
)
Input : $array1 = array("aim", "plan", "vision", "clarity")
$array2 = array("word1" => "loneliness", "word2" => "happiness")
$array3 = array(0 => "solitude")
array_replace($array1, $array2, $array3)
Output : Array
(
[0] => solitude
[1] => plan
[2] => vision
[3] => clarity
[word1] => loneliness
[word2] => happiness
)
In the first example the key,
0 is present in both the arrays, therefore its value is replaced with the one in which it occurs last i.e.
grape and the key
4 is present in the second array therefore its value is also replaced.
In the second example the key
0 is present in the third array, therefore its value is replaced in the first array. The keys
word1 and
word2 are not present in the first array, therefore they are added to the first array along with their values.
Below programs illustrate the array_replace() function in PHP:
Program 1:
PHP
<?php
// Array to be replaced
$array1 = array("orange", "banana", "apple",
"raspberry");
// arrays that will replace the values
// in the first array
$array2 = array(0 => "pineapple", 4 => "cherry");
$array3 = array(0 => "grape");
$resArr = array_replace($array1, $array2,
$array3);
print_r($resArr);
?>
Output:
Array
(
[0] => grape
[1] => banana
[2] => apple
[3] => raspberry
[4] => cherry
)
Program 2:
PHP
<?php
// Array to be replaced
$array1 = array("aim", "plan", "vision", "clarity");
// arrays that will replace the values
// in the first array
$array2 = array("word1" => "loneliness",
"word2" => "happiness");
$array3 = array(0 => "solitude");
$resArr = array_replace($array1, $array2,
$array3);
print_r($resArr);
?>
Output:
Array
(
[0] => solitude
[1] => plan
[2] => vision
[3] => clarity
[word1] => loneliness
[word2] => happiness
)
Reference:
http://php.net/manual/en/function.array-replace.php
Similar Reads
PHP array_chunk() Function
The array_chunk() function is an inbuilt function in PHP which is used to split an array into parts or chunks of given size depending upon the parameters passed to the function. The last chunk may contain fewer elements than the desired size of the chunk. Syntaxarray array_chunk( $array, $size, $pre
2 min read
PHP array_combine() Function
The array_combine() function is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values. That is all elements of one array will be the keys of new array and all elements of the second array will be the values of t
2 min read
PHP array_count_values() Function
The array_count_values() is an inbuilt function in PHP which is used to count all the values inside an array. In other words we can say that array_count_values() function is used to calculate the frequency of all of the elements of an array. Syntax: array array_count_values( $array ) Parameters: Thi
2 min read
PHP array_diff_assoc() Function
This inbuilt function of PHP is used to get the difference between one or more arrays. This function compares both the keys and values between one or more arrays and returns the difference between them. So, the function generally compares two arrays according to there keys and values and returns the
2 min read
PHP array_diff_key() Function
This inbuilt function of PHP is used to get the difference between one or more arrays. This function compares the keys between one or more arrays and returns the difference between them. So, the function generally compares two arrays according to their keys and returns the elements that are present
2 min read
PHP array_diff_uassoc() Function
The array_diff_uassoc() function is a built-in function in PHP and is used to get the difference between one or more arrays using an user-defined function to compare the keys. This function compares both the keys and values between one or more arrays to and returns the elements from the first array
4 min read
PHP array_diff_ukey() Function
The array_diff_ukey() function is an inbuilt function in PHP. It is used to compares the key two or more arrays using a user-defined function, and return an array, which is array1 and its not present any others array2, array3 or more... Syntax: array_diff_ukey($array1, $array2, $array3..., arr_diffu
4 min read
PHP array_diff() function
The array_diff() is an inbuilt function in PHP ans is used to calculate the difference between two or more arrays. This function computes difference according to the values of the elements, between one or more array and return differences in the form of a new array. This function basically returns a
2 min read
PHP array_fill_keys() Function
The array_fill_keys() function is a builtin function in PHP and is used to create a new array filled with the given keys and value provided as an array to the function. Syntax: array array_fill_keys ( $keys, $value ) Parameters: This function accepts two parameters, keys and their values to be prese
3 min read
PHP array_fill() function
The array_fill() is an inbuilt-function in PHP and is used to fill an array with values. This function basically creates an user-defined array with a given pre-filled value. Syntax: array_fill($start_index, $number_elements, $values) Parameter: The array_fill() function takes three parameters and ar
2 min read