Home >>PHP Array Functions >PHP array_merge_recursive() Function

PHP array_merge_recursive() Function

PHP array_merge_recursive() Function

PHP array_merge_recursive() Function is used to merge two or more arrays into a single array means this function is used to combine two or more arrays of elements or values into a single array together.

The combining takes place in such a way that at the end of the preceding array the values of one array are appended. If the given arrays contain the same keys, then a value is assigned to the key which has an array consisting of the same key values.

Syntax:

array_merge_recursive(array1, array2, array3, ...)

Parameter Values

Parameter Description
array1 This is required parameter.This parameter specifies an array.
array2 This is optional parameter. This parameter specifies an array.
array3,... This is optional parameter. This parameter specifies an array.

Here, is an example of array_merge_recursive() Function in PHP

<html>
<body>
<?php
$a1=array("a"=>"white","b"=>"black");
$a2=array("c"=>"blue","b"=>"voilet");
$a3=array("c"=>"green","a"=>"red");
print_r(array_merge_recursive($a1,$a2,$a3));
?>
</body>
</html>
Output:
Array ( [a] => Array ( [0] => white [1] => red ) [b] => Array ( [0] => black [1] => voilet ) [c] => Array ( [0] => blue [1] => green ) )

Example 2:

<html>
<body>
<?php
$a1=array("a"=>"white","b"=>"black");
$a2=array("c"=>"blue","b"=>"voilet");
print_r(array_merge_recursive($a2,$a1));
?>
</body>
</html>
Output:
Array ( [c] => blue [b] => Array ( [0] => voilet [1] => black ) [a] => white )

No Sidebar ads