Skip to content

Commit 5cc10e8

Browse files
authored
Simplify array_replace description and add nested array example (#3809)
Based on discussion at PR #1470
1 parent 6c550e6 commit 5cc10e8

1 file changed

Lines changed: 52 additions & 19 deletions

File tree

reference/array/functions/array-replace.xml

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,13 @@
1313
<methodparam rep="repeat"><type>array</type><parameter>replacements</parameter></methodparam>
1414
</methodsynopsis>
1515
<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.
2419
</para>
2520
<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.
2823
</para>
2924
</refsect1>
3025
<refsect1 role="parameters">
@@ -70,21 +65,59 @@ $replacements = array(0 => "pineapple", 4 => "cherry");
7065
$replacements2 = array(0 => "grape");
7166
7267
$basket = array_replace($base, $replacements, $replacements2);
73-
print_r($basket);
68+
var_dump($basket);
7469
?>
7570
]]>
7671
</programlisting>
7772
&example.outputs;
7873
<screen role="php">
7974
<![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+
}
88121
]]>
89122
</screen>
90123
</example>

0 commit comments

Comments
 (0)