-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22.7 Array_replace & Array_replace_recursive.php
51 lines (32 loc) · 1.27 KB
/
22.7 Array_replace & Array_replace_recursive.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
// array_replace()
// Index or Associative Array
// If a key from array1 exists in array2, values from array1 will be replaced by the values from array2
// Use array_replace when you only need to replace values in the top-level elements of an array.
Syntax:
// array_replace(array1, array2, array3, ...)
// array_replace used Index array
$fruit1 = ['orange', 'banana', 'apple', 'grapes'];
$color = ['red', 'green', 'blue'];
$newArray1 = array_replace($fruit1, $color);
echo "<pre>";
print_r($newArray1);
echo "<pre>";
// array_replace used associative array
$fruit = ['orange', 'banana', 'a' => 'apple', 'grapes'];
$veggie = ['a' => 'carrot', 1 => 'pea'];
$newArray = array_replace($fruit, $veggie);
echo "<pre>";
print_r($newArray);
echo "</pre>";
//***************************************************************************************************************** */
// Array_replace_recursive()
// Use array_replace_recursive when you need to replace values within nested arrays as well.
//
// Multidemensional Associative Array
$array1 = array("a" => array("red"), "b" => array("green", "pink"));
$array2 = array("a" => array("yellow"), "b" => array("black"));
$newArray1 = array_replace_recursive($array1, $array2);
echo "<pre>";
print_r($newArray1);
echo "</pre>";