PHP array_replace_recursive() Function
In this chapter you will learn:
- Definition for PHP array_replace_recursive() Function
- Syntax for PHP array_replace_recursive() Function
- Parameter for PHP array_replace_recursive() Function
- Example for PHP array_replace_recursive() Function
- Example for PHP array_replace_recursive() Function with Multiple arrays
- Example - Differences between array_replace() and array_replace_recursive()
Definition
The array_replace_recursive() function replaces the values of the first array with the values from following arrays recursively.
Syntax
PHP array_replace_recursive() Function has the following syntax.
array_replace_recursive(array1,array2,array3...)
Parameter
Parameter | Is Required | Description |
---|---|---|
array1 | Required. | Array to be replaced |
array2 | Optional. | Array to replace array1 |
array3,... | Optional. | Array to replace array before array3(array1,array2) |
Example 1
Replaces the values of the first array with the values from following arrays recursively
<?php// j a v a 2s . co m
$a1=array("a"=>array("A"),"b"=>array("Java","PHP"),);
$a2=array("a"=>array("B"),"b"=>array("HTML"));
print_r(array_replace_recursive($a1,$a2));
?>
The code above generates the following result.
Example 2
Multiple arrays:
<?php/*from j a v a 2s. co m*/
$a1=array("a"=>array("A"),"b"=>array("Java","PHP"),);
$a2=array("a"=>array("B"),"b"=>array("HTML"));
$a3=array("a"=>array("C"),"b"=>array("CSS"));
print_r(array_replace_recursive($a1,$a2,$a3));
?>
The code above generates the following result.
Example 3
Differences between array_replace() and array_replace_recursive():
<?php//from j av a 2 s . com
$a1=array("a"=>array("A"),"b"=>array("Java","PHP"),);
$a2=array("a"=>array("B"),"b"=>array("HTML"));
$result=array_replace_recursive($a1,$a2);
print_r($result);
$result=array_replace($a1,$a2);
print_r($result);
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP array_reverse() Function
- Syntax for PHP array_reverse() Function
- Parameter for PHP array_reverse() Function
- Example for PHP array_reverse() Function
- Example - Preserve the keys during the reverse
Home » PHP Tutorial » PHP Array Functions