AppDividend
Latest Code Tutorials

PHP Array Replace Example | array_replace() Function Tutorial

0

PHP Array Replace Example | array_replace() Function Tutorial is today’s topic. PHP array_replace() function replace the values of the first array with the values from the second array. 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 single array to the function or as many arrays as you like. It totally depends upon your requirement.

PHP Array Replace Example

If the key from array1 exists in array2, then the values from array1 will be replaced by the values from an array2. If a key only exists in an array1, it will be left as it is. See the syntax below.

Syntax

array_replace(array1,array2,array3...)

The array1 parameter is required.

The array2 parameter is optional, and it specifies an array which 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.

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, then 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 array_replace() function on them. 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, then it will output from both the arrays combined. So, the output is 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

 

Conclusively, PHP Array Replace Example | array_replace() Function Tutorial article is over.

Leave A Reply

Your email address will not be published.

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