array_replace


php128 apg

REPLACE elements from passed ARRAYS into the FIRST ARRAY.





This function replaces the values of $array1 with values having the same keys in each of 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 first, 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 not recursive, this means that it will replace values in the $array1 by whatever type is in the $array2.

Values from later arrays overwrite the previous values.

This function returns NULL if an error occurs.



<?php

arr array_replace 
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 
= [ 9101112 ];

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

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

$arr1abc array_replace ($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 ($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 ($arr1a, $arr1b, $arr1c):
Array ( [0] => seven [1] => eight [2] => 11 [3] => 12 )

Values with coincident keys were replaced by elements of a rear array.

Values with different keys, were kept in the resulting array.


  2 EXERCISE   

<?php

$arr2a 
= [ => "five"=> "six" ];

$arr2b = [ => "seven"=> "eight" ];

$arr2c = [ => 9=> 10=> 11=> 12 ];

$arr2abc array_replace ($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 
'ARRAY after array_replace:<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 ($arr2a, $arr2b, $arr2c):
Array ( [1] => five [2] => six [3] => seven [4] => eight [5] => 9 [6] => 10 [7] => 11 [8] => 12 )

No value has key matching.

Values with different keys, were kept in the resulting array.


  3 EXERCISE   

<?php

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

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

$arr3c = [ 9101112 ];

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 
f32 ($var1$var2)
{
$arr12 array_replace($var1$var2);
echo 
'<br>- - - - - - - - - - - - - -<br>After array_replace(<pre>';
var_dump($var1);
echo 
', ';
var_dump($var2);
echo 
'</pre>):<br><br>Resulting:<br><pre>';
var_dump($arr12);
echo 
'</pre><br>- - - - - - - - - - - - - -<br>';
}

function 
f33 ($var1$var2$var3)
{
$arr13 array_replace($var1$var2$var3);
echo 
'<br>- - - - - - - - - - - - - -<br>After array_replace(<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>';
}

f32($arr3a$arr3b);

f32($arr3b$arr3a);

f32($arr3a$arr3c);

f32($arr3c$arr3a);

f32($arr3b$arr3c);

f32($arr3c$arr3b);


f33($arr3a$arr3b$arr3c);

f33($arr3a$arr3c$arr3b);

f33($arr3b$arr3a$arr3c);

f33($arr3b$arr3c$arr3a);

f33($arr3c$arr3a$arr3b);

f33($arr3c$arr3b$arr3a);

?> 

  4 EXERCISE   

<?php

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

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

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

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

echo 
'The given $array1:<br>';
var_dump($array1);

echo 
'<br><br>The given $array2:<br>';
var_dump($array2);

echo 
'<br><br>The given $array3:<br>';
var_dump($array3);

echo 
'<br><br>The given $array4:<br>';
var_dump($array4);

echo 
'<br><br><br>Testing array_replace($array1, $array2)<br>';
$data array_replace($array1$array2);

var_dump($data);

echo 
'<br><br>Testing array_replace($array3, $array4)<br>';
$data1 array_replace($array3$array4);

var_dump($data1);

?>

  5 EXERCISE   

<?php

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

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

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

$array4 = array('one'"two");
$array4[] = &$array4;

echo 
'The given $array1:<br>';
var_dump($array1);

echo 
'<br><br>The given $array2:<br>';
var_dump($array2);

echo 
'<br><br>The given $array3:<br>';
var_dump($array3);

echo 
'<br><br>The given $array4:<br>';
var_dump($array4); 

echo 
'<br><br><br>Testing array_replace($array1, $array2):<br>';
$data array_replace($array1$array2);
var_dump($data);

echo 
'<br><br><br>Testing array_replace($array3, $array4):<br>';
$data array_replace($array3$array4);
var_dump($data);

?>