PHP Array Replace Recursive


 

PHP Array Replace Recursive

PHP Array Replace Recursive: In this tutorial you will come to know about replacing the values of an array recursively. PHP provides array_replace_recursive() to replace the values of first array with the following array, general description on this function is given in the tutorial which is easy to understand. Several examples are present in this tutorial.

PHP Array Replace Recursive: In this tutorial you will come to know about replacing the values of an array recursively. PHP provides array_replace_recursive() to replace the values of first array with the following array, general description on this function is given in the tutorial which is easy to understand. Several examples are present in this tutorial.

PHP Array Replace Recursive:

PHP provides array_replace_recursive() function to replace elements from passed arrays into the first array recursively. array_replace_recursive() replaces the values of first array with the same values from following arrays. If a key is present in both first array and in the second array, value of the second array will replace the values of first array. If a key is present in first array but not in  the second array it will be untouched and if a key is present in second array but not in the first array, this array will be created in the first array.

General description of array_merge() is: 

General Format array array_replace_recursive(array $var1 , array $var2 [,array $var3[,..,]])
Parameters $var1: First array in which the elements is to be replaced $var(2,3,..):Next arrays from which the values to be extracted.
Return Value Returns the resulting array

 

PHP Array Replace Recursive Example 1:

<?php

$array1=array("b"=>array("beneath","benign"),"c"=>"contemporary","d"=>"diligent");

$array2=array("b"=>array("boon"),"c"=>"control");

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

var_dump($array1);

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

var_dump($array2);

 

$array3=array_replace_recursive($array1,$array2);

 

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

var_dump($array3);

 

$array3=array_replace($array1,$array2);

 

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

var_dump($array3);

?>

 

Output:

Initially the values of $array1 is:
array(3) {
  ["b"]=>
  array(2) {
    [0]=>
    string(7) "beneath"
    [1]=>
    string(6) "benign"
  }
  ["c"]=>
  string(12) "contemporary"
  ["d"]=>
  string(8) "diligent"
}

Initially the values of $array2 is:
array(2) {
  ["b"]=>
  array(1) {
    [0]=>
    string(4) "boon"
  }
  ["c"]=>
  string(7) "control"
}

After replacing recursively the values of $array3 is:
array(3) {
  ["b"]=>
  array(2) {
    [0]=>
    string(4) "boon"
    [1]=>
    string(6) "benign"
  }
  ["c"]=>
  string(7) "control"
  ["d"]=>
  string(8) "diligent"
}

After replacing normally the values of $array3 is:
array(3) {
  ["b"]=>
  array(1) {
    [0]=>
    string(4) "boon"
  }
  ["c"]=>
  string(7) "control"
  ["d"]=>
  string(8) "diligent"
}

Example 2:

<?php

$array1=array("b"=>array("beneath","benign"),"d"=>"diligent");

$array2=array("b"=>array("boon"),"c"=>"control");

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

0

var_dump($array1);

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

var_dump($array2);

1

 

$array3=array_replace_recursive($array1,$array2);

 

2

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

var_dump($array3);

 

3

$array3=array_replace($array1,$array2);

 

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

4

var_dump($array3);

?>

Output:

5
Initially the values of $array1 is:
array(2) {
  ["b"]=>
  array(2) {
    [0]=>
    string(7) "beneath"
    [1]=>
    string(6) "benign"
  }
  ["d"]=>
  string(8) "diligent"
}

Initially the values of $array2 is:
array(2) {
  ["b"]=>
  array(1) {
    [0]=>
    string(4) "boon"
  }
  ["c"]=>
  string(7) "control"
}

After replacing recursively the values of $array3 is:
array(3) {
  ["b"]=>
  array(2) {
    [0]=>
    string(4) "boon"
    [1]=>
    string(6) "benign"
  }
  ["d"]=>
  string(8) "diligent"
  ["c"]=>
  string(7) "control"
}

After replacing normally the values of $array3 is:
array(3) {
  ["b"]=>
  array(1) {
    [0]=>
    string(4) "boon"
  }
  ["d"]=>
  string(8) "diligent"
  ["c"]=>
  string(7) "control"
}

Example 3:

<?php

$array1=array("beneath","benign","diligent");

6

$array2=array("boon","control");

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

var_dump($array1);

7

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

var_dump($array2);

 

8

$array3=array_replace_recursive($array1,$array2);

 

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

9

var_dump($array3);

 

$array3=array_replace($array1,$array2);

0

 

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

var_dump($array3);

1

?>

Output:

Initially the values of $array1 is:
array(3) {
  [0]=>
  string(7) "beneath"
  [1]=>
  string(6) "benign"
  [2]=>
  string(8) "diligent"
}

Initially the values of $array2 is:
array(2) {
  [0]=>
  string(4) "boon"
  [1]=>
  string(7) "control"
}

After replacing recursively the values of $array3 is:
array(3) {
  [0]=>
  string(4) "boon"
  [1]=>
  string(7) "control"
  [2]=>
  string(8) "diligent"
}

After replacing normally the values of $array3 is:
array(3) {
  [0]=>
  string(4) "boon"
  [1]=>
  string(7) "control"
  [2]=>
  string(8) "diligent"
}

2

Example 4:

 

 

 

3

 

Ads