PHP Programing language

array_replace() Function in PHP
Previous Home Next

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

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: