PHP | array_replace() Function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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);
  
?>

chevron_right


Output:

Array
(
    [0] => grape
    [1] => banana
    [2] => apple
    [3] => raspberry
    [4] => cherry
)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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);
  
?>

chevron_right


Output:

Array
(
    [0] => solitude
    [1] => plan
    [2] => vision
    [3] => clarity
    [word1] => loneliness
    [word2] => happiness
)

Reference:
http://php.net/manual/en/function.array-replace.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.