Home Tutorial Php Phpbasics PHP Array Replace

 
 

Share on Google+Share on Google+
PHP Array Replace
Posted on: October 27, 2009 at 12:00 AM
Advertisement
PHP Array Replace: In this tutorial you will learn how to replace values of one array with another. PHP provides array_replace() function to replace the values. array_replace() replaces the value of the first array with the same value of the given array or list of arrays.

PHP Array Replace

Sometimes we have to replace array values with given values, PHP provides array_replace() function to replace the values. array_replace() replaces the value of the first array with the same value of the given array or list of arrays. The value of first array will be replaced by the second array's value if  key matched. If a key is present in the second array but not in the first array, then it will be created in the first array.

General description of array_replace() is: 

General Format array array_replace(array $array1,array $array2[,$array3[,..]])
Parameters array $array1: The array in which the elements are added array $array2,3,..,n: The array from which the values are taken
Return Value an array or null

 

Example 1:

<?php

$array=array("new delhi","calcutta","bombay","madras");

echo"<br/><b>Initially the values of \$array1 is:</b><br/>";

var_dump($array);

$repl1=array(1=>"kolkata",2=>"mumbai");

echo"<br/><b>The values of \$repl1 is:</b><br/>";

var_dump($repl1);

$repl2=array(3=>"chennai");

echo"<br/><b>The values of \$repl2 is:</b><br/>";

var_dump($repl2);

$city=array_replace($array,$repl1,$repl2);

echo"<br/><b>After the values of \$city is:</b><br/>";

var_dump($city);

?>

Output:

Initially the values of $array1 is:
array(4) {
  [0]=>
  string(9) "new delhi"
  [1]=>
  string(8) "calcutta"
  [2]=>
  string(6) "bombay"
  [3]=>
  string(6) "madras"
}

The values of $repl1 is:
array(2) {
  [1]=>
  string(7) "kolkata"
  [2]=>
  string(6) "mumbai"
}

The values of $repl2 is:
array(1) {
  [3]=>
  string(7) "chennai"
}

After the values of $city is:
array(4) {
  [0]=>
  string(9) "new delhi"
  [1]=>
  string(7) "kolkata"
  [2]=>
  string(6) "mumbai"
  [3]=>
  string(7) "chennai"
}

Example 2 (Same as previous, numeric array):

<?php

$array=array("new delhi","calcutta","bombay","madras");

echo"<br/><b>Initially the values of \$array1 is:</b><br/>";

var_dump($array);

$repl1=array("kolkata","mumbai");

echo"<br/><b>The values of \$repl1 is:</b><br/>";

var_dump($repl1);

$repl2=array("chennai");

echo"<br/><b>The values of \$repl2 is:</b><br/>";

var_dump($repl2);

$city=array_replace($array,$repl1,$repl2);

echo"<br/><b>After the values of \$city is:</b><br/>";

var_dump($city);

?>

Output:

Initially the values of $array1 is:
array(4) {
  [0]=>
  string(9) "new delhi"
  [1]=>
  string(8) "calcutta"
  [2]=>
  string(6) "bombay"
  [3]=>
  string(6) "madras"
}

The values of $repl1 is:
array(2) {
  [0]=>
  string(7) "kolkata"
  [1]=>
  string(6) "mumbai"
}

The values of $repl2 is:
array(1) {
  [0]=>
  string(7) "chennai"
}

After the values of $city is:
array(4) {
  [0]=>
  string(7) "chennai"
  [1]=>
  string(6) "mumbai"
  [2]=>
  string(6) "bombay"
  [3]=>
  string(6) "madras"
}

Example 3:

<?php

$array=array("new delhi"=>1,"calcutta"=>2,"bombay"=>3,"madras"=>4);

echo"<br/><b>Initially the values of \$array1 is:</b><br/>";

var_dump($array);

$repl1=array("calcutta"=>33,"mumbai"=>22);

echo"<br/><b>The values of \$repl1 is:</b><br/>";

var_dump($repl1);

$repl2=array("madras"=>44);

echo"<br/><b>The values of \$repl2 is:</b><br/>";

var_dump($repl2);

$city=array_replace($array,$repl1,$repl2);

echo"<br/><b>After the values of \$city is:</b><br/>";

var_dump($city);

?>

Output:

Initially the values of $array1 is:
array(4) {
  ["new delhi"]=>
  int(1)
  ["calcutta"]=>
  int(2)
  ["bombay"]=>
  int(3)
  ["madras"]=>
  int(4)
}

The values of $repl1 is:
array(2) {
  ["calcutta"]=>
  int(33)
  ["mumbai"]=>
  int(22)
}

The values of $repl2 is:
array(1) {
  ["madras"]=>
  int(44)
}

After the values of $city is:
array(5) {
  ["new delhi"]=>
  int(1)
  ["calcutta"]=>
  int(33)
  ["bombay"]=>
  int(3)
  ["madras"]=>
  int(44)
  ["mumbai"]=>
  int(22)
}
Advertisement

Related Tags for PHP Array Replace:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 27, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com