array_replace_recursive


php128 apg

REPLACE elements from passed ARRAYS into the FIRST ARRAY recursively.





This function replaces the values of $array1 with the same values from all the following arrays.

If a key from the $array1 exists in the $array2, its value will be replaced by the value from the $array2.

If the key exists in the $array2, and not the $array1, it will be created in the $array1.

If a key exists in the $array2 an not in the $array1, it will be created in the $array1.

If a key only exists in the $array1, it will be left as is.

If $array2, $array3, ..., $arrayN are passed for replacement, they will be processed in order, the later arrays overwriting the previous values.

This function is recursive, this means that it will recurse into arrays and apply the same process to the inner value.

When the value in the $array1 is scalar, it will be replaced bu the value in the $array2 may it be scalar or array.

When the value in the $array1 and $array2 are both arrays, this function will replace their respective value recursively.

This function returns NULL if an error occurs.



<?php

arr array_replace_recursive 
( arr $array1 [, 
                                              
arr $array2, ..., arr $arrayN ] )
 

where,

$array1 = The first ARRAY in which elements are replaced

$array2 
= The second ARRAY in which elements will be extracted

. . . . . . . . . . . .

$arrayN = The last ARRAY in which elements will be extracted

?>
 

$array1


The first ARRAY in which elements are replaced.



$array2


The second ARRAY in which elements will be extracted .



$arrayN


The last ARRAY in which elements will be extracted.



  1 EXERCISE   

<?php

$arr1a 
= [ 9, 10, 11, 12 ];

$arr1b = [ "five", "six" ];

$arr1c = [ "seven", "eight" ];

$arr1abc = array_replace_recursive ($arr1a, 
                                                        
$arr1b, 
                                                        
$arr1c);

echo 
'First ARRAY:<br>';
print_r($arr1a);
echo 
'<br><br>';

echo 
'Second ARRAY:<br>';
print_r($arr1b);
echo 
'<br><br>';

echo 
'Third ARRAY:<br>';
print_r($arr1c);
echo 
'<br><br><br>';

echo 
'After array_replace_recursive($arr1a, 
                                                        $arr1b, 
                                                        $arr1c):<br>'
;
print_r($arr1abc);

?> 

 RESULT   

First ARRAY:
Array ( [0] => 9 [1] => 10 [2] => 11 [3] => 12 )

Second ARRAY:
Array ( [0] => five [1] => six )

Third ARRAY:
Array ( [0] => seven [1] => eight )


After array_replace_recursive($arr1a, $arr1b, $arr1c):
Array ( [0] => seven [1] => eight [2] => 11 [3] => 12 )


  2 EXERCISE   

<?php

$arr2a 
= [ 1 => "five", 2 => "six" ];

$arr2b = [ 3 => "seven", 4 => "eight" ];

$arr2c = [ 5 => 9, 6 => 10, 7 => 11, 8 => 12 ];

$arr2abc = array_replace_recursive ($arr2a, 
                                                         
$arr2b, 
                                                         
$arr2c);

echo 
'First ARRAY:<br>';
print_r($arr2a);
echo 
'<br><br>';

echo 
'Second ARRAY:<br>';
print_r($arr2b);
echo 
'<br><br>';

echo 
'Third ARRAY:<br>';
print_r($arr2c);
echo 
'<br><br><br>';

echo 
'After array_replace_recursive ($arr2a, 
                                                         $arr2b, 
                                                         $arr2c):<br>'
;
print_r($arr2abc);

?> 

 RESULT   

First ARRAY:
Array ( [1] => five [2] => six )

Second ARRAY:
Array ( [3] => seven [4] => eight )

Third ARRAY:
Array ( [5] => 9 [6] => 10 [7] => 11 [8] => 12 )


After array_replace_recursive ($arr2a, $arr2b, $arr2c):
Array ( [1] => five [2] => six [3] => seven [4] => eight [5] => 9 [6] => 10 [7] => 11 [8] => 12 )


  3 EXERCISE   

 <?php

$arr3a 
= [ 1 => "five", 2 =>  [ "six", "seven", "eight" ] ];

$arr3b = [ "seven", "eight" ];

$arr3c = [ 9, 10, 11, 12 ];

echo 
'First ARRAY:<br>';
print_r($arr3a);
echo 
'<br><br>';

echo 
'Second ARRAY:<br>';
print_r($arr3b);
echo 
'<br><br>';

echo 
'Third ARRAY:<br>';
print_r($arr3c);
echo 
'<br><br><br>';

function 
f32r ($var1, $var2)
{
$arr12 = array_replace_recursive($var1, $var2);
echo 
'<br>- - - - - - - - - - - - - -<br>
After array_replace_recursive(<pre>'
;
var_dump($var1);
echo 
', ';
var_dump($var2);
echo 
'</pre>):<br><br>Resulting:<br><pre>';
var_dump($arr12);
echo 
'</pre><br>- - - - - - - - - - - - - -<br>';
}

function 
f33r ($var1, $var2, $var3)
{
$arr13 = array_replace_recursive($var1, $var2, $var3);
echo 
'<br>- - - - - - - - - - - - - -<br>
After array_replace_recursive(<pre>'
;
var_dump($var1);
echo 
', ';
var_dump($var2);
echo 
', ';
var_dump($var3);
echo 
'</pre>):<br><br>Resulting:<br><pre>';
var_dump($arr13);
echo 
'</pre><br>- - - - - - - - - - - - - -<br>';
}

f32r($arr3a, $arr3b);

f32r($arr3b, $arr3a);

f32r($arr3a, $arr3c);

f32r($arr3c, $arr3a);

f32r($arr3b, $arr3c);

f32r($arr3c, $arr3b);


f33r($arr3a, $arr3b, $arr3c);

f33r($arr3a, $arr3c, $arr3b);

f33r($arr3b, $arr3a, $arr3c);

f33r($arr3b, $arr3c, $arr3a);

f33r($arr3c, $arr3a, $arr3b);

f33r($arr3c, $arr3b, $arr3a);

?>  

  4 EXERCISE   

<?php

$arr4a 
= [ 1 => [ "four", "five" ], 2 =>  [ "six", "seven", "eight" ] ];

$arr4b = [ 3 => "seven", 4 => "eight" ];

$arr4ab = array_replace_recursive ($arr4a, $arr4b);

$arr4ba = array_replace_recursive ($arr4b, $arr4a);


echo 
'First ARRAY - $arr4a:<br>';
print_r($arr4a);
echo 
'<br><br>';

echo 
'Second ARRAY - $arr4b:<br>';
print_r($arr4b);
echo 
'<br><br>';

echo 
'After array_replace_recursive($arr4ab):<br>';
print_r($arr4ab);
echo 
'<br><br>';

echo 
'After array_replace_recursive($arr4ba):<br>';
print_r($arr4ba);
echo 
'<br>';

?> 

  5 EXERCISE   

 <?php


$arr5ar 
= [ 1 => "five", 2 => [ "six", "seven", "eight" ] ];

echo 
'ARRAY $arr5ar:<br>';
print_r($arr5ar);
echo 
'<br><br>';

$arr5br = [ "seven", "eight" ];

echo 
'ARRAY $arr5br:<br>';
print_r($arr5br);
echo 
'<br><br>';

echo 
'ARRAY array_replace_recursive($arr5ar, $arr5br):<br>';
$arr5abr = array_replace_recursive ($arr5ar, $arr5br);
print_r($arr5abr);
echo 
'<br><br><br>';

$arr5cr = [ 9, 10, 11, 12 ];
echo 
'ARRAY $arr5cr:<br>';
print_r($arr5cr);
echo 
'<br><br>';

$arr5abcr = array_replace_recursive ($arr5abr, $arr5cr);
echo 
'After array_replace_recursive($arr5abr, $arr5cr):<br>';
print_r($arr5abcr);
echo 
'<br><br>';

$arr5crab = array_replace_recursive ($arr5abr, $arr5cr);
echo 
'After array_replace_recursive($arr5cr, $arr5abr):<br>';
print_r($arr5crab);
echo 
'<br><br>';

?> 

  6 EXERCISE   

<?php

$array1 
= array(
    
0 => 'dontclobber',
    
'1' => 'unclobbered',
    
'test2' => 0.0,
    
'test3' => array(
        
'testarray2' => true,
        
1 => array(
            
'testsubarray1' => 'dontclobber2',
            
'testsubarray2' => 'dontclobber3',
    ),
    ),
);

$array2 = array(
    
1 => 'clobbered',
    
'test3' => array(
        
'testarray2' => false,
    ),
    
'test4' => array(
        
'clobbered3' => array(0, 1, 2),
    ),
);

$array3 = array(array(array(array())));

$array4 = array();
$array4[] = &$array4;

echo 
"Testing array_replace():<br>";
$data = array_replace($array1, $array2);

var_dump($data);

echo 
"<br><br>Testing array_replace_recursive():<br>";
$data = array_replace_recursive($array1, $array2);

var_dump($data);

echo 
"<br><br>With endless recusrsion:<br>";
$data = array_replace_recursive($array3, $array4);

var_dump($data);

?>

  7 EXERCISE   

<?php

$array1 
= array(
    
0 => 'dontclobber',
    
'1' => 'unclobbered',
    
'test2' => 0.0,
    
'test3' => array(
        
'testarray2' => true,
        
1 => array(
            
'testsubarray1' => 'dontclobber2',
            
'testsubarray2' => 'dontclobber3',
    ),
    ),
);

$array2 = array(
    
1 => 'clobbered',
    
'test3' => array(
        
'testarray2' => false,
    ),
    
'test4' => array(
        
'clobbered3' => array(0, 1, 2),
    ),
);

$array3 = array(array(array(array())));

$array4 = array();
$array4[] = &$array4;

echo 
"Testing array_replace():<br>";
$data = array_replace($array1, $array2);

var_dump($data);

echo 
"<br><br>Testing array_replace_recursive():<br>";
$data = array_replace_recursive($array1, $array2);

var_dump($data);

echo 
"<br><br>With endless recusrsion:<br>";
$data = array_replace_recursive($array3, $array4);

var_dump($data);

?>

  8 EXERCISE   

<?php

$one 
= [1];
$two = [42];
$arr1 = ['k' => &$one];
$arr2 = ['k' => &$two];
var_dump(current($one), current($two));
array_replace_recursive($arr1, $arr2);
var_dump(current($one), current($two));
echo 
'<br><br>';

$one = [1];
$two = [42];
$arr1 = ['k' => &$one];
$arr2 = ['k' => &$two];
var_dump(current($one), current($two));
array_merge_recursive($arr1, $arr2);
var_dump(current($one), current($two));

?>