array_merge_recursive


php128 apg

MERGES the elements of one or more ARRAYS together so that the values of one are appended to the end of the previous one, recursively.





If $array1, $array2, ..., $arrayN have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too.

If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended

An array of values resulted from merging the arguments together.

If this function called without any arguments, returns an empty ARRAY.

In PHP 7.4.0, this function can now be called without any parameter but, formerly, at least one parameter has been required.



<?php

arr array_merge_recursive 
( [ arr $array2, ..., $arrayN ] )
 

where,


$array1 The first ARRAY of the list of ARRAYS to recusively merge
                                    
$array2 
The second ARRAY of the list of ARRAYS to recusively merge

. . . . . . . . . . .

$arrayN The last ARRAY of the list of ARRAYS to recusively merge

?>
 

$array1


The first ARRAY of the list of ARRAYS to recusively merge.



$array2


The second ARRAY of the list of ARRAYS to recusively merge.



$arrayN


The last ARRAY of the list of ARRAYS to recusively merge.



  1 EXERCISE   

<?php

$nbr01a 
= [ 123456];

$nbr01b =   [ 8910 ];

$nbr01c =   [ 1112131415 ];

echo 
'FIRST ARRAY<br><pre>';
print_r($nbr01a);
echo 
'</pre><br><br>';

echo 
'SECOND ARRAY<br><pre>';
print_r($nbr01b);
echo 
'</pre><br><br>';

echo 
'THIRD ARRAY<br><pre>';
print_r($nbr01c);
echo 
'</pre><br><br>';

echo 
'ARRAY after array_merge_recursive<br><br>';
$nbr01abc 
array_merge_recursive ($nbr01a$nbr01b$nbr01c);

echo 
'<pre>';
print_r($nbr01abc);
echo 
'</pre><br><br>';

?> 

 RESULT   

FIRST ARRAY
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
)

SECOND ARRAY
Array
(
[0] => 8
[1] => 9
[2] => 10
)

THIRD ARRAY
Array
(
[0] => 11
[1] => 12
[2] => 13
[3] => 14
[4] => 15
)

ARRAY after array_merge_recursive
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
[10] => 11
[11] => 12
[12] => 13
[13] => 14
[14] => 15


The same result as obtained with array_merge.


  2 EXERCISE   

 <?php

$arrm02a 
= [ 'countries' => [ 'Brasil''Portugal' ] ];

$arrm02b = [ 'countries' => [ 'Argentina''Espanha' ] ];

echo 
'INITIAL ARRAYS:<br><pre>';
print_r($arrm02a);
echo 
'</pre><br><br><pre>';
print_r($arrm02b);

$arrm02ab array_merge($arrm02a$arrm02b);

echo 
'<br><br><br>RESULTING ARRAY after array_merge:<br><pre>';
print_r($arrm02ab);
echo 
'</pre>';

$arrmr02ab array_merge_recursive($arrm02a$arrm02b);

echo 
'<br><br><br>RESULTING ARRAY after array_merge_recursive:<br><pre>';
print_r($arrmr02ab);
echo 
'</pre>';

?> 

 RESULT   

INITIAL ARRAYS:
Array
(
[countries] => Array
(
[0] => Brasil
[1] => Portugal
)
)

Array
(
[countries] => Array (
[0] => Argentina
[1] => Espanha
)
)

ARRAY after array_merge
Array
(
[countries] => Array (
[0] => Argentina
[1] => Espanha
)
)


ARRAY after array_merge_recursive
Array
(
[countries] => Array (
[0] => Brasil
[1] => Portugal
[2] => Argentina
[3] => Espanha
)
)


This exercise shows the difference of action between array_merge and array_merge_recursive.


  3 EXERCISE   

<?php


$arrmr005 
= array("blue""yellow""white");

$arrmr006 = array("green");

echo 
'The given ARRAY(1):<br>';
print_r($arrmr005);
echo 
'<br><br>The given ARRAY(2):<br>';
print_r($arrmr006);

$arrmr056 array_merge_recursive($arrmr005$arrmr006);

echo 
'<br><br>RESULTING ARRAY after array_merge_recursive:<br>';
print_r($arrmr056);

?>

 RESULT   

The given ARRAY(1):
Array ( [0] => blue [1] => yellow [2] => white )

The given ARRAY(2):
Array ( [0] => green )

RESULTING ARRAY after array_merge_recursive:
Array ( [0] => blue [1] => yellow [2] => white [3] => green )

In this case, it didn't matter that the indexes of the arrays involved were repeated.

Same result as array_merge.


  4 EXERCISE   

<?php

$arrmr007 
= [2282020'countries' => ['Brasil''Portugal']];

$arrmr008 = ['continents' => ['South America''Europa']];

echo 
'The given ARRAY(1):<br><pre>';
print_r($arrmr007);
echo 
'</pre><br><br>The given ARRAY(2):<br><pre>';
print_r($arrmr008);

$arrmr078 
array_merge_recursive($arrmr007$arrmr008);

echo 
'</pre><br><br>RESULTING ARRAY after array_merge_recursive:<br><pre>';
print_r($arrmr078);
echo 
'</pre>';

?>

  5 EXERCISE   

<?php

$arrmr009 
= ['countries' => ['Brasil''Portugal']];

$arrmr00A = ['COUNTRIES' => ['Argentina''Espanha']];

echo 
'The given ARRAY(1):<br>';
print_r($arrmr009);
echo 
'<br><br>The given ARRAY(2):<br>';
print_r($arrmr00A);

$arrmr09R array_merge_recursive($arrmr009$arrmr00A);

echo 
'<br><br>RESULTING ARRAY after array_merge_recursive:<br>';
print_r($arrmr09R);

$arrmr09S array_merge($arrmr009$arrmr00A);

echo 
'<br><br>RESULTING ARRAY after array_merge:<br>';
print_r($arrmr09S);

?>

  6 EXERCISE   

<?php
echo "Testing array with default keys:<br><br>";

// Initialise the arrays
$arr1 = array(1, array(12));
$arr2 = array(3, array("hello"'world'));
$arr3 = array(array(67), array("str1"'str2'));

echo 
'The given ARRAY(1):<br>';
var_dump($arr1);

echo 
'<br><br>The given ARRAY(2):<br>';
var_dump($arr2);

echo 
'<br><br>The given ARRAY(3):<br>';
var_dump($arr1);

// Calling array_merge_recursive() 
// without arguments
echo "Without arguments:<br>";
var_dumparray_merge_recursive() );

// Calling array_merge_recursive() 
// with default arguments
echo "<br><br>With default argument => [\$arr1]:<br>";
var_dumparray_merge_recursive($arr1) );

// Calling array_merge_recursive() 
// with more arguments
echo "<br><br>With more arguments => [\$arr1, \$arr2]:<br>";
var_dumparray_merge_recursive($arr1,$arr2) );
echo 
"<br><br>
         With more arguments => [\$arr1, \$arr2, \$arr3]:<br>"
;
var_dumparray_merge_recursive($arr1,$arr2,$arr3) );

?>

  7 EXERCISE   

<?php

echo "Testing associative arrays:<br><br>";

// Initialise the arrays
$arr1 = array(=> "one"=> array(12));
$arr2 = array(=> 'three'"four" => array("hello"'world'));
$arr3 = array(=> array(67), 'four' => array("str1"'str2'));

echo 
'The given ARRAY(1):<br>';
var_dump($arr1);

echo 
'<br><br>The given ARRAY(2):<br>';
var_dump($arr2);

echo 
'<br><br>The given ARRAY(3):<br>';
var_dump($arr1);

// Calling array_merge_recursive() 
// with default arguments
echo "<br><br>With default argument:<br>";
var_dumparray_merge_recursive($arr1) );

// Calling array_merge_recursive() 
// with more arguments
echo "<br><br>With more arguments => [\$arr1, \$arr2]:<br>";
var_dumparray_merge_recursive($arr1,$arr2) );
echo 
"<br><br>
         With more arguments => [\$arr1, \$arr2, \$arr3]:<br>"
;
var_dumparray_merge_recursive($arr1,$arr2,$arr3) );

?>

  8 EXERCISE   

<?php

/*
 * Testing the functionality of 
 * array_merge_recursive() by passing
 * two dimensional arrays for $arr1 argument.
*/

echo "Testing two dimensional array for \$arr1 argument:<br><br>";

// initialize the 2-d array
$arr1 = array(
  array(
1231),
  
"array" => array("hello""world""str1" => "hello"
                                                      
"str2" => 'world'),
  array(
=> "one"=> "two""one"'two'),
  array(
1231)
);

// initialize the second argument
$arr2 = array(1"hello""array" => array("hello"'world'));

echo 
'The given ARRAY(1):<br>';
var_dump($arr1);

echo 
'<br><br>The given ARRAY(2):<br>';
var_dump($arr2);

echo 
"<br><br>Passing the entire 2-d array.<br>";
echo 
"With default argument:<br>";
var_dumparray_merge_recursive($arr1) );

echo 
"<br><br>With more arguments => [\$arr1, \$arr2]:<br>";
var_dumparray_merge_recursive($arr1,$arr2) );

echo 
"<br><br>Passing the sub-array.<br>";
echo 
"With default argument => \$arr1[\"array\"]:<br>";
var_dumparray_merge_recursive($arr1["array"]) );
echo 
"<br><br>
With more arguments => \$arr1[\"array\"], \$arr2[\"array\"]:<br>"
;
var_dumparray_merge_recursive($arr1["array"], 
                                                      
$arr2["array"]) );
?>

  9 EXERCISE   

<?php

/*
* Passing different arrays 
* to $arr1 argument and testing whether
* array_merge_recursive() behaves 
* in an expected way.
*/

echo "Passing different arrays to \$arr1 argument:<br><br>";

/* Different heredoc strings */

// heredoc with blank line
$blank_line = <<<EOT


EOT;

// heredoc with multiline string
$multiline_string = <<<EOT
hello world
The quick brown fox jumped over;
the lazy dog
This is a double quoted string
EOT;

// heredoc with different whitespaces
$diff_whitespaces = <<<EOT
hello\r world\t
1111\t\t != 2222\v\v
heredoc\ndouble quoted string.
 with\vdifferent\fwhite\vspaces
EOT;

// heredoc with quoted strings and numeric values
$numeric_string = <<<EOT
11 < 12. 123 >22
'single quoted string'
"double quoted string"
2222 != 1111.\t 0000 = 0000\n
EOT;

// arrays passed to $arr1 argument
$arrays = array (
/*1*/  array(12,), 
       
// with default keys and numeric values
       
array(1.12.2),
       
// with default keys & float values
       
array(falsetrue),
       
// with default keys and boolean values
       
array(),
       
// empty array
/*5*/  
array(NULL),
       
// with NULL
       
array("a\v\f""aaaa\r""b""\[\]\!\@\#\$\%\^\&\*\(\)\{\}"),
       
// with double quoted strings
       
array('a\v\f''aaaa\r''b''\[\]\!\@\#\$\%\^\&\*\(\)\{\}'),
       
// with single quoted strings
       
array("h1" => $blank_line"h2" => $multiline_string
                                                
"h3" => $diff_whitespaces),
       
// with heredocs

       // associative arrays
/*9*/  
array(=> "one"=> "two"),
       
// explicit numeric keys, string values
       
array("one" => 1"two" => 2"1" => ),
       
// string keys & numeric values
       
array( => 10=> 20=> 40),
       
// explicit numeric keys and numeric values
       
array( "one" => "ten""two" => "twenty"),
       
// string key/value
       
array("one" => 1=> "two"=> "four"),
       
//mixed

       // associative array, 
       // containing null/empty/boolean values as key/value
/*14*/ 
array(NULL => "NULL"null => "null"
                                   
"NULL" => NULL"null" => null),
       array(
true => "true"false => "false"
                                       
"false" => false"true" => true),
       array(
"" => "emptyd"'' => 'emptys'
                                              
"emptyd" => ""'emptys' => ''),
       array(
=> ''=> ""=> NULL=> null
                                                       
=> false=> true),
       array(
'' => 1"" => 2NULL => 3
                                      
null => 4false => 5true => 6),

       
// array containing embedded arrays
/*15*/ 
array("str1""array" => array("hello"'world'), 
                                              array(
12))
);

// initialise the second argument
$arr2 = array( => "one"2"string" => "hello"
                                    
"array" => array("a""b""c"));

// loop through each sub array of 
// $arrays and check the behavior of 
// array_merge_recursive()
$iterator 1;
foreach(
$arrays as $arr1) {
  echo 
"<br><br>Iteration: $iterator<br>";

  
// with default argument
  
echo "<br>With default argument:<br>";
  
var_dumparray_merge_recursive($arr1) );

  
// with more arguments
  
echo "<br><br>With more arguments:<br>";
  
var_dumparray_merge_recursive($arr1$arr2) );

  
$iterator++;
}

?>

  10 EXERCISE   

<?php

/*
 * Testing the functionality of 
 * array_merge_recursive() by passing
 * array having duplicate keys.
*/

echo "Testing array with duplicate keys for \$arr1 argument:<br>";

/* initialize the array having duplicate keys */
// array with numeric keys
$arr1_numeric_key = array( => "one"
                             
=> "two"
                             
=> array(12), 
                             
=> "three"
                             
=> array("duplicate"'strings'));
// array with string keys
$arr1_string_key = array("str1" => "hello"
                                   
"str2" => 111
                                   
"str1" => "world"
                                   
"str2" => 111.111);

// initialize the second argument
$arr2 = array("one""str1" => "two", array("one""two"));

echo 
"<br><br>With default argument:<br>";
var_dumparray_merge_recursive($arr1_numeric_key) );
echo 
'<br><br>';
var_dumparray_merge_recursive($arr1_string_key) );

echo 
"<br><br>With more arguments:<br>";
var_dumparray_merge_recursive($arr1_numeric_key$arr2) );
echo 
'<br><br>';
var_dumparray_merge_recursive($arr1_string_key$arr2) );

?>

  11 EXERCISE   

<?php

/*
 * Testing the functionality of 
 * array_merge_recursive() by passing
 * array having reference variables.
*/

echo "Testing array with reference variables 
                                for \$arr1 argument:<br><br>"
;

$value1 10;
$value2 "hello";
$value3 0;
$value4 = &$value2;

// input array containing elements as reference variables
$arr1 = array(
  
=> 0,
  
=> &$value4,
  
=> &$value2,
  
=> "hello",
  
=> &$value3,
  
$value4 => &$value2
);

// initialize the second argument
$arr2 = array($value4 => "hello", &$value2);

echo 
"With default argument => \$arr1:<br>";
var_dumparray_merge_recursive($arr1) );

echo 
"<br><br>With more arguments => \$arr1, \$arr2:<br>";
var_dumparray_merge_recursive($arr1$arr2) );

?>

  12 EXERCISE   

<?php

/*
 * Testing the functionality of 
 * array_merge_recursive() by passing 
 * an array having binary values.
*/

echo "Testing array with binary data for \$arr1 argument:<br><br>";

// array with binary values
$arr1 = array(b"1"b"hello" => "hello"
                      
b"world""str1" => b"hello"
                     
"str2" => "world");

// initialize the second argument
$arr2 = array(b"str1" => b"binary"
                      
b"hello" => "binary"
                      
b"str2" => b"binary");

echo 
'The given ARRAY(1):<br>';
var_dump($arr1);

echo 
'<br><br>The given ARRAY(2):<br>';
var_dump($arr2);

echo 
"<br><br>With default argument => \$arr1:<br>";
var_dumparray_merge_recursive($arr1) );

echo 
"<br><br>With more arguments => \$arr1, \$arr2:<br>";
var_dumparray_merge_recursive($arr1$arr2) );

?>

  13 EXERCISE   

<?php

/*
 * Testing the functionality of 
 * array_merge_recursive() by passing
 * arrays having common key and value.
*/

echo "Testing arrays with common key and value:<br><br>";

/* initialize the array having duplicate values */

// integer values
$arr1 = array("a" => 1"b" => 2);
$arr2 = array("b" => 2"c" => 4);

echo 
'- - - - - - - - - - - - - - - - -<br>
                 The given ARRAY with INTEGER values(1):<br>'
;
var_dump($arr1);

echo 
'<br><br>The given ARRAY with INTEGER values(2):<br>';
var_dump($arr2);

echo 
"<br><br>INTEGER values;<br>";
var_dumparray_merge_recursive($arr1$arr2) );

// float values
$arr1 = array("a" => 1.1"b" => 2.2);
$arr2 = array("b" => 2.2"c" => 3.3);

echo 
'<br>- - - - - - - - - - - - - - - - -<br>
              The given ARRAY with FLOAT values(1):<br>'
;
var_dump($arr1);

echo 
'<br><br>The given ARRAY with FLOAT values(2):<br>';
var_dump($arr2);

echo 
"<br><br>FLOAT values:<br>";
var_dumparray_merge_recursive($arr1$arr2) );

// string values
$arr1 = array("a" => "hello""b" => "world");
$arr2 = array("b" => "world""c" => "string");

echo 
'<br>- - - - - - - - - - - - - - - - -<br>
               The given ARRAY with STRING values(1):<br>'
;
var_dump($arr1);

echo 
'<br><br>The given ARRAY with STRING values(2):<br>';
var_dump($arr2);

echo 
"<br><br>STRING values:<br>";
var_dumparray_merge_recursive($arr1$arr2) );

// boolean values
$arr1 = array("a" => true"b" => false);
$arr2 = array("b" => false);

echo 
'<br>- - - - - - - - - - - - - - - - -<br>
           The given ARRAY with BOOLEAN values(1):<br>'
;
var_dump($arr1);

echo 
'<br><br>The given ARRAY with BOOLEAN values(2):<br>';
var_dump($arr2);

echo 
"<br><br>BOOLEAN values:<br>";
var_dumparray_merge_recursive($arr1$arr2) );

// null values
$arr1 = array( "a" => NULL);
$arr2 = array( "a" => NULL);

echo 
'<br>- - - - - - - - - - - - - - - - -<br>
             The given ARRAY with NULL values(1):<br>'
;
var_dump($arr1);

echo 
'<br><br>The given ARRAY with NULL values(2):<br>';
var_dump($arr2);

echo 
"<br><br>NULL values:<br>";
var_dumparray_merge_recursive($arr1$arr2) );
echo 
'<br>- - - - - - - - - - - - - - - - -<br>';

?>