PHP Programing language

adplus-dvertising
array_replace() Function in PHP
Previous Home Next
adplus-dvertising

array_replace()

The array_replace() function replaces the values of the first array with the values from following arrays.

Syntax:
array_replace(array1,array2,array3...)

In above syntax, "array1" specifies an array, "array2" specifies an array which will replace the values of array1 and "array3,..." specifies more arrays to replace the values of array1 and array2, etc. Values from later arrays will overwrite the previous ones.

Example:

<?php
$arr1=array("a"=>"Apple","b"=>"Mango");
$arr2=array("a"=>"Banana","Orange");
print_r(array_replace($arr1,$arr2));
?>

Output:

Array ( [a] => Banana [b] => Mango [0] => Orange )

array_replace_recursive()

The array_replace_recursive() function replaces the values of the first array with the values from following arrays recursively.

Syntax:
array_replace_recursive(array1,array2,array3...)

In above syntax, "array1" specifies an array, "array2" specifies an array which will replace the values of array1 and "array3,..." specifies more arrays to replace the values of array1 and array2, etc. Values from later arrays will overwrite the previous ones.

Example:

<?php
$arr1=array("a"=>array("Apple"),"b"=>array("Mango","Banana"));
$arr2=array("a"=>array("Orange"),"b"=>array("Guava"));
$arr3=array("a"=>array("Papaya"),"b"=>array("Pomigranate"));
print_r(array_replace_recursive($arr1,$arr2,$arr3));
?>

Output:

Array ( [a] => Array ( [0] => Papaya ) [b] => Array ( [0] => Pomigranate [1] => Banana ) )

Previous Home Next