Tags
PHP
Asked 2 years ago
18 May 2021
Views 350
Cimb

Cimb posted

PHP array_replace() Function

how array_replace() Function work in php ?
joomler

joomler
answered Nov 30 '-1 00:00

array_replace() function replace the value of from array between


<?php
$pro=array("programming"=>"java","script"=>"javascript");
$taste=array("dcode"=>"php","programming"=>"tough",);
print_r(array_replace($pro,$taste));
?>

result will be Array ( [programming] => tough [script] => javascript [dcode] => php )
it means array_replace() function rewrite array with first to last , common key name will overwritten and other get merged



<?php
$pro=array("programming"=>"java","script"=>"javascript");
$taste=array("dcode"=>"php","allcode"=>"tough",);
print_r(array_replace($pro,$taste));
?>

suppose in above code there is no common key between so it gets merged two array
so result will be Array ( [programming] => java [script] => javascript [dcode] => php [allcode] => tough )

lets see how array_replace() function with multidimensional array

<?php
 
$pro=array("programming"=>"java","script"=>"javascript");
$taste=array("dcode"=>"php","allcode"=>"tough",);
$tested=array("0"=>array("php","dcode"=>"java"),
 "dcode"=>array("php"),
	"allcode"=>"asdf",);

print_r(array_replace($pro,$taste,$tested));
 ?>


the common key between the arrays than last array value will be returned and the other remains untouched.

Array ( [programming] => java [script] => javascript [dcode] => Array ( [0] => php ) [allcode] => asdf [0] => Array ( [0] => php [dcode] => java ) )
Post Answer