Merge array recursively in PHP

If you want merge array recursively in php then you can use array_merge_recursive function. It will merge all the array elements having same string keys and it returns the resulting array.

Syntax:

array array_merge ( array $array1 [, array $... ] )

Note :-

Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

Example:-

<?php

$array1 = array('key0' => 'val11', 'key1' => 'val21', 'key2' => 'val31');
$array2 = array('key0' => 'val12', 'key1' => 'val22', 'key2' => 'val32');
$array3 = array('key0' => 'val13', 'key1' => 'val23', 'key2' => 'val33');

$result = array_merge_recursive($array1, $array2, $array3);
echo '<pre>';
print_r($result);
?>

Output:-

Array
(
   [key0] => Array
   (
     [0] => val11
     [1] => val12
     [2] => val13
   )

   [key1] => Array
   (
     [0] => val21
     [1] => val22
     [2] => val23
   )

   [key2] => Array
   (
     [0] => val31
     [1] => val32
     [2] => val33
   )

)

array merge recursively if array keys are integer

example:-


<?php
$array1 = array(0 => 'val11', 1 => 'val21', 2 => 'val31');
$array2 = array(0 => 'val12', 1 => 'val22', 2 => 'val32');
$array3 = array(0 => 'val13', 1 => 'val23', 2 => 'val33');

$result = array_merge_recursive($array1, $array2, $array3);

echo '<pre>';
print_r($result);

?>

output:-


Array
(
    [0] => val11
    [1] => val21
    [2] => val31
    [3] => val12
    [4] => val22
    [5] => val32
    [6] => val13
    [7] => val23
    [8] => val33
)

As I have mentioned in note above “Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.”

If you want to merge array recursively on integer keys then below solution will works for you.

Array merge recursively on integer keys:-

<?php
$array1 = array(0 => 'val11', 1 => 'val21', 2 => 'val31');
$array2 = array(0 => 'val12', 1 => 'val22', 2 => 'val32');
$array3 = array(0 => 'val13', 1 => 'val23', 2 => 'val33');

// Add prefix in array key
function addPrefix($a) {
return '_' . $a;
}

# transform keys
$array1 = array_combine(array_map('addPrefix', array_keys($array1)), $array1);
$array2 = array_combine(array_map('addPrefix', array_keys($array2)), $array2);
$array3 = array_combine(array_map('addPrefix', array_keys($array3)), $array3);

$result = array_merge_recursive($array1, $array2, $array3);

# Remove prefix from array keys
function removePrefix($a) {
  return substr($a, 1);
}

$array = array_combine(array_map('removePrefix', array_keys($result)), $result);
echo '<pre>';
print_r($result);
?>

output:-

 >Array
(
    [_0] => Array
        (
            [0] => val11
            [1] => val12
            [2] => val13
        )

    [_1] => Array
        (
            [0] => val21
            [1] => val22
            [2] => val23
        )

    [_2] => Array
        (
            [0] => val31
            [1] => val32
            [2] => val33
        )

)

Array Merge recursively with callback function ( if you want merge final array with comma separated value )


$array1 = array('key0' => 'val11', 'key1' => 'val21', 'key2' => 'val31');
$array2 = array('key0' => 'val12', 'key1' => 'val22', 'key2' => 'val32');
$array3 = array('key0' => 'val13', 'key1' => 'val23', 'key2' => 'val33');

$result = array_map(function($x) {
return join(',', $x);
}, call_user_func_array('array_merge_recursive', array($array1, $array2, $array3)));

echo '<pre>';
print_r($result);

output:


Array
(
    [key0] => val11,val12,val13
    [key1] => val21,val22,val23
    [key2] => val31,val32,val33
)
(Visited 161 times, 5 visits today)