AppDividend
Latest Code Tutorials

PHP Array Replace: How to Replace Array Values in PHP

If the key from array1 exists in array2, then the values from array1 will be replaced by the values from array2. On the other hand, if a key only exists in array1, it will be left as it is.

PHP array_replace()

PHP array_replace() is a built-in function that replaces the values of the first array with the values from the second array. For example, the array_replace() method replaces the values of the first array with the values from the second array or the following arrays. You can assign a single array to the function or as many arrays as you like. It totally depends upon your requirements.

Syntax

array_replace(array1,array2,array3...)

Arguments

The array1 parameter is required.

The array2 parameter is optional, and it specifies an array that will replace the values of array1.

The array3 parameter is optional, and it specifies more arrays to replace the values of array1 and array2, etc.

Example

See the following code example.

<?php

// app.php

$brandsA = [
  'a' => 'Louis Vuitton',
  'b' => 'Hermès',
  'c' => 'Gucci',
  'd' => 'Prada',
  'e' => 'Chanel',
  'f' => 'Cartier'
];
$brandsB = [
  'b' => 'Tesla',
  'd' => 'Coca-cola',
  'e' => 'Google',
  'f' => 'Facebook'
];
$replaced = array_replace($brandsA, $brandsB);
print_r($replaced);

In the code, we have defined two arrays. Now, we are replacing some of the items of brandsA with brandsB. If the keys of a brandsA match with brandsB, it will replace the values of brandsA with brandsB. See the below output.

PHP Array Replace Example

If the key exists in array2 and not in array1

Let’s see the scenario where the key exists in array1 and not in array2 and use the array_replace() function. See the code.

<?php

// app.php

$brandsA = [
  'a' => 'Louis Vuitton',
  'b' => 'Hermès',
  'c' => 'Gucci',
  'd' => 'Prada',
  'e' => 'Chanel',
  'f' => 'Cartier'
];
$brandsB = [
  'x' => 'Tesla',
  'y' => 'Coca-cola',
  'z' => 'Google',
  'w' => 'Facebook'
];
$replaced = array_replace($brandsA, $brandsB);
print_r($replaced);

In the above code, both brandsA and brandsB have different keys. That is why when we use the array_replace() function, it will output from both the arrays combined. So, the output is the following.

PHP array_replace() Function Tutorial

Using three arrays in PHP array_replace()

If we use three arrays, then the last array($array3) the last array will overwrite the previous ones ($a1 and $a2).

See the below code.

<?php

// app.php

$brandsA = [
  'a' => 'Louis Vuitton',
  'b' => 'Hermès',
  'c' => 'Gucci',
  'd' => 'Prada',
  'e' => 'Chanel',
  'f' => 'Cartier'
];
$brandsB = [
  'x' => 'Tesla',
  'y' => 'Coca-cola',
  'z' => 'Google',
  'w' => 'Facebook'
];

$brandsC = [
  'a' => 'Rolex',
  'b' => 'Zara',
  'y' => 'Pepsico',
  'z' => 'Alibaba',
];
$replaced = array_replace($brandsA, $brandsB, $brandsC);
print_r($replaced);

See the output below.

Using three arrays in PHP array_replace()

Using numeric keys

If a key exists in arrA and not in arrB. See the following code.

<?php

// app.php

$arrA = ['disney', 'netflix', 'hulu'];
$arrB = [0 => 'sports', 1 => 'games', 2 => 'discovery'];

$replaced = array_replace($arrA, $arrB);
print_r($replaced);

See the output.

Using numeric keys in PHP array_replace() function

That’s it for this tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.