PHP array_replace() function

P
PHP array_reduce() function
PHP array_replace_recursive() function

The array_replace function replaces the values from the first array with the values of the following arrays given to the function. You will get a clear understanding after going through the syntax and examples below.

You can assign one or multiple arrays to the array_replace function.

There are the following scenarios to be focused on for the array_replace function.

  1. If a key exist in array 1 found in the array 2, it will be replaced by the value of array 2.
  2. If the array 1 key does not exist in any of the following arrays, it will remain same in the result.
  3. If array 1 does not contains any key that the following arrays do, they will be created in the array 1.
  4. If multiple arrays are used, the latest array value will overwrite the value in the previous array.

What is the syntax of the array_replace function in PHP?

array_replace(array1, array2, array3, ...)
ParameterDescription
array1Array to replace the values of – Required
array2Array to replace the values with – Optional
array3,…More arrays to replace the values. Values from the last most array overwrite the previous ones – Optional
array_replace function in PHP

Examples of array_replace function

<?php
$array_1=array("Green","Red");
$array_2=array("Yellow","Blue");
print_r(array_replace($array_1,$array_2));
?>

In the above example, we replace the values of one array with the values of another array.

<?php
$array_1=array("key1"=>"Red","key2"=>"Green");
$array_2=array("key1"=>"Blue","Yellow");
print_r(array_replace($a1,$a2));
?>

The above example shows the scenario in which the key of the first array exists in the second array so it will be replaced by the second array value. Also, the first array contains a key that does not exist in the second array. You must execute this script and see the output.

<?php
$array_1=array("a"=>"1","2");
$array_2=array("a"=>"3","b"=>"4");
print_r(array_replace($a1,$a2));
?>

In the above example, keys exist in array 2 but not in the first array.

<?php
$array_1=array("R","G");
$array_2=array("B","Y");
$array_3=array("O","B");
print_r(array_replace($array_1,$array_2,$array_3));
?>

The above example shows that the last array will replace the values of the previous two arrays.

<?php
$array_1=array("1","2","3","4");
$array_2=array(0=>"O",3=>"B");
print_r(array_replace($array_1,$array_2));
?>

The above example shows that the numeric key exists in the second array but not in the first array.

What is the difference between array_replace and array_replace_recursive function?

Understand and execute the following example script to have a clear difference between the array_replace and array_replace_recursive function.

<?php
$array_1=array("a"=>array("1"),"b"=>array("2","3"),);
$array_2=array("a"=>array("4"),"b"=>array("5"));

$res=array_replace_recursive($array_1,$array_2);
print_r($res);

$result=array_replace($array_1,$array_2);
print_r($res);
?>
PHP array_reduce() function
PHP array_replace_recursive() function

Tutor Network

Share this

Learn PHP from A to Z