PHP Array Merge Recursive


 

PHP Array Merge Recursive

PHP Array Merge Recursive: In this tutorial we will learn PHP array_merge_recursive() function. Difference between PHP array_merge() and array_merge_recursive() is that if two or more than two elements have the same key then instead of override, it makes an array of that key.

PHP Array Merge Recursive: In this tutorial we will learn PHP array_merge_recursive() function. Difference between PHP array_merge() and array_merge_recursive() is that if two or more than two elements have the same key then instead of override, it makes an array of that key.

PHP Array Merge Recursive

The PHP array_merge_recursive() function is same as the array_merge() function. It creates an array by appending each input array to the previous array. The main Difference between array_merge() and array_merge_recursive() is that if two or more than two elements have the same key then instead of override, it makes an array of that key. 

General description of array_merge_recursive() is: 

General Format array array_merge_recursive(array $var1 [, array $var2 [,.....]])
Parameters $varn: the arrays to be merge

 

Return Value Returns the resulting array

 

PHP Array Merge Recursive Example 1:

<?php

$array1=array("a"=>"anxious","b"=>"breathe","c"=>"contrary");

echo"<br/><b>Initially the values of \$array1 is:</b><br/>";

var_dump($array1);

$array2=array("c"=>"contemporary","d"=>"diligent");

echo"<br/><b>Initially the values of \$array2 is:</b><br/>";

var_dump($array2);

echo"<br/><b>After merging two arrays the values of \$array1 is:</b><br/>";

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

print_r($array1);

?>

Output:

Initially the values of $array1 is:
array(3) {
  ["a"]=>
  string(7) "anxious"
  ["b"]=>
  string(7) "breathe"
  ["c"]=>
  string(8) "contrary"
}

Initially the values of $array2 is:
array(2) {
  ["c"]=>
  string(12) "contemporary"
  ["d"]=>
  string(8) "diligent"
}

After merging two arrays the values of $array1 is:
Array
(
    [a] => anxious
    [b] => breathe
    [c] => Array
        (
            [0] => contrary
            [1] => contemporary
        )

    [d] => diligent
)

Example 2:

<?php

$array1=array("a"=>"anxious","b"=>"breathe","c"=>"contrary");

echo"<br/><b>Initially the values of \$array1 is:</b><br/>";

var_dump($array1);

$array2=array("b"=>array("beneath","benign"),"c"=>"contemporary","d"=>"diligent");

echo"<br/><b>Initially the values of \$array2 is:</b><br/>";

var_dump($array2);

echo"<br/><b>After merging two arrays the values of \$array1 is:</b><br/>";

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

print_r($array1);

?>

 

0

Output:

Initially the values of $array1 is:
array(3) {
  ["a"]=>
  string(7) "anxious"
  ["b"]=>
  string(7) "breathe"
  ["c"]=>
  string(8) "contrary"
}

Initially the values of $array2 is:
array(3) {
  ["b"]=>
  array(2) {
    [0]=>
    string(7) "beneath"
    [1]=>
    string(6) "benign"
  }
  ["c"]=>
  string(12) "contemporary"
  ["d"]=>
  string(8) "diligent"
}

After merging two arrays the values of $array1 is:
Array
(
    [a] => anxious
    [b] => Array
        (
            [0] => breathe
            [1] => beneath
            [2] => benign
        )

    [c] => Array
        (
            [0] => contrary
            [1] => contemporary
        )

    [d] => diligent
)

 

 

1

Ads