reference/array/functions/array-replace.xml
5cc10e8d90a9d0cbb35779b60580e772e7d660a1
5cc10e8d90a9d0cbb35779b60580e772e7d660a1
...
...
@@ -9,22 +9,17 @@
9
9
&reftitle.description;
10
10
<methodsynopsis>
11
11
<type>array</type><methodname>array_replace</methodname>
12
-
<methodparam><type>array</type><parameter>array1</parameter></methodparam>
13
-
<methodparam choice="opt"><type>array</type><parameter>...</parameter></methodparam>
12
+
<methodparam><type>array</type><parameter>array</parameter></methodparam>
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>array1</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">
...
...
@@ -32,7 +27,7 @@
32
27
<para>
33
28
<variablelist>
34
29
<varlistentry>
35
-
<term><parameter>array1</parameter></term>
30
+
<term><parameter>array</parameter></term>
36
31
<listitem>
37
32
<para>
38
33
The array in which elements are replaced.
...
...
@@ -40,7 +35,7 @@
40
35
</listitem>
41
36
</varlistentry>
42
37
<varlistentry>
43
-
<term><parameter>...</parameter></term>
38
+
<term><parameter>replacements</parameter></term>
44
39
<listitem>
45
40
<para>
46
41
Arrays from which elements will be extracted.
...
...
@@ -54,7 +49,7 @@
54
49
<refsect1 role="returnvalues">
55
50
&reftitle.returnvalues;
56
51
<para>
57
-
Returns an <type>array</type>, or &null; if an error occurs.
52
+
Returns an <type>array</type>.
58
53
</para>
59
54
</refsect1>
60
55
<refsect1 role="examples">
...
...
@@ -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>
...
...
@@ -100,7 +133,6 @@ Array
100
133
</para>
101
134
</refsect1>
102
135
</refentry>
103
-
104
136
<!-- Keep this comment at the end of the file
105
137
Local variables:
106
138
mode: sgml
107
139