PHP array_replace() function

PHP array_replace() function replaces elements of first array with other passed array. The array_replace() function is PHP built-in function.

array_replace() replaces the values of array with values having the same keys in each of the following arrays. If the key exists in the second array but not in the first, it will be created in the first array.

Syntax :

array_replace ( array $array , array $replacement1, array $replacement2  , ........);

Parameter,

$array : It is first array . It is used to replace value of it.

$replacement1 :  It is replacement array which is used to replace value of first array.

$replacement2 :  It is replacement array which is used to replace value of first array and first replacement array.

Note : Later array can replace value of previously array in array_replace function.

Return : The array_replace() function  returns array with replaced value. It will return null if an error occurs.

Let's see below example to understand php array_replace() Function in details.

At below example, array_replace() function return replaced array $tutorials after replacing value of $array1 by arrays $array2 and $array3.

Example :

<?php
$array1 = array("php","html","css","java","mysql");
$array2 = array(0=>"python",4=>"oracle");
$array3=array("asp",5=>"linux");
$tutorials = array_replace($array1,$array2,$array3);
echo "<br>Look at below first array before replacing value<br>";
print_r($array1);
echo "<br>Look at below replaced array $tutorials after replacing value of $array1 by arrays $array2 and $array3 <br>";
print_r($tutorials);
?>

Output :

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

15723