|
13 | 13 | <methodparam rep="repeat"><type>array</type><parameter>replacements</parameter></methodparam> |
14 | 14 | </methodsynopsis> |
15 | 15 | <para> |
16 | | - <function>array_replace</function> replaces the values of |
17 | | - <parameter>array</parameter> with values having the same keys in each of the following |
18 | | - arrays. If a key from the first array exists in the second array, its value |
19 | | - will be replaced by the value from the second array. If the key exists in the |
20 | | - second array, and not the first, it will be created in the first array. |
21 | | - If a key only exists in the first array, it will be left as is. |
22 | | - If several arrays are passed for replacement, they will be processed |
23 | | - in order, the later arrays overwriting the previous values. |
| 16 | + <function>array_replace</function> creates a new array and assigns items into |
| 17 | + it for each key in each of the provided arrays. If a key appears in multiple |
| 18 | + input arrays, the value from the right-most input array will be used. |
24 | 19 | </para> |
25 | 20 | <para> |
26 | | - <function>array_replace</function> is not recursive : it will replace |
27 | | - values in the first array by whatever type is in the second array. |
| 21 | + <function>array_replace</function> does not process elements items recursively, |
| 22 | + it replaces the entire value for each key when it does a replacement. |
28 | 23 | </para> |
29 | 24 | </refsect1> |
30 | 25 | <refsect1 role="parameters"> |
@@ -70,21 +65,59 @@ $replacements = array(0 => "pineapple", 4 => "cherry"); |
70 | 65 | $replacements2 = array(0 => "grape"); |
71 | 66 |
|
72 | 67 | $basket = array_replace($base, $replacements, $replacements2); |
73 | | -print_r($basket); |
| 68 | +var_dump($basket); |
74 | 69 | ?> |
75 | 70 | ]]> |
76 | 71 | </programlisting> |
77 | 72 | &example.outputs; |
78 | 73 | <screen role="php"> |
79 | 74 | <![CDATA[ |
80 | | -Array |
81 | | -( |
82 | | - [0] => grape |
83 | | - [1] => banana |
84 | | - [2] => apple |
85 | | - [3] => raspberry |
86 | | - [4] => cherry |
87 | | -) |
| 75 | +array(5) { |
| 76 | + [0]=> |
| 77 | + string(5) "grape" |
| 78 | + [1]=> |
| 79 | + string(6) "banana" |
| 80 | + [2]=> |
| 81 | + string(5) "apple" |
| 82 | + [3]=> |
| 83 | + string(9) "raspberry" |
| 84 | + [4]=> |
| 85 | + string(6) "cherry" |
| 86 | +} |
| 87 | +]]> |
| 88 | + </screen> |
| 89 | + </example> |
| 90 | + <example> |
| 91 | + <title>Example of how nested arrays are handled</title> |
| 92 | + <programlisting role="php"> |
| 93 | +<![CDATA[ |
| 94 | +<?php |
| 95 | +$base = [ 'citrus' => [ 'orange', 'lemon' ], 'pome' => [ 'apple' ] ]; |
| 96 | +$replacements = [ 'citrus' => [ 'grapefruit' ] ]; |
| 97 | +$replacements2 = [ 'citrus' => [ 'kumquat', 'citron' ], 'pome' => [ 'loquat' ] ]; |
| 98 | +
|
| 99 | +$basket = array_replace($base, $replacements, $replacements2); |
| 100 | +var_dump($basket); |
| 101 | +?> |
| 102 | +]]> |
| 103 | + </programlisting> |
| 104 | + &example.outputs; |
| 105 | + <screen role="php"> |
| 106 | +<![CDATA[ |
| 107 | +array(2) { |
| 108 | + ["citrus"]=> |
| 109 | + array(2) { |
| 110 | + [0]=> |
| 111 | + string(7) "kumquat" |
| 112 | + [1]=> |
| 113 | + string(6) "citron" |
| 114 | + } |
| 115 | + ["pome"]=> |
| 116 | + array(1) { |
| 117 | + [0]=> |
| 118 | + string(6) "loquat" |
| 119 | + } |
| 120 | +} |
88 | 121 | ]]> |
89 | 122 | </screen> |
90 | 123 | </example> |
|
0 commit comments