PHP | array_replace_recursive() Function
The array_replace_recursive() is an inbuilt function in PHP that replaces the values of the first array with the values from following arrays recursively.
It performs the replacement based on the below rules:
- If a key from the first array exists in the second array, then the value corresponding to that key of the first array will be replaced by the value of the second array.
- If the key exists in the second array but not in the first array then it will be created in the first array.
- If a key only exists in the first array then it will be left as it is.
- If several arrays are passed for replacement, they will be processed in order, the later array overwriting the previous values.
Syntax:
array_replace_recursive($array1, $array2, $array3...)
Parameters: This function accepts a list of arrays as parameters where the first parameter is compulsory and rest are optional.
Return Value: It returns the modified array, or NULL if an error occurs.
Example:
Input: $array1 = array("a"=>array("red"), "b"=>array("green")); $array2 = array("a"=>array("yellow"), "b"=>array("black")); Output: Array ( [a] => Array ( [0] => yellow ) [b] => Array ( [0] => black ) )
Below program illustrate the array_replace_recursive() function:
<?php // PHP program to illustrate array_replace_recursive() // function $array1 = array ( "a" => array ( "red" ), "b" => array ( "green" , "blue" )); $array2 = array ( "a" => array ( "yellow" ), "b" => array ( "black" )); $array3 = array ( "a" => array ( "orange" ), "b" => array ( "burgundy" )); print_r(array_replace_recursive( $array1 , $array2 , $array3 )); ?> |
Output:
Array ( [a] => Array ( [0] => orange ) [b] => Array ( [0] => burgundy [1] => blue ) )
Reference:
http://php.net/manual/en/function.array-replace-recursive.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- PHP | end() Function
- D3.js | d3.map.set() Function
- D3.js | d3.set.add() Function
- D3.js | d3.max() function
- p5.js | sq() function
- PHP | abs() Function
- p5.js | pow() function
- PHP | pos() Function
- PHP | key() Function
- D3.js | d3.sum() function
- D3.js | d3.mean() function
- p5.js | red() function
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.
Improved By : Akanksha_Rai