reference/array/functions/array-filter.xml
31cacb6f262f455c616094cfe6d09bc0d1df2748
...
...
@@ -11,15 +11,19 @@
11
11
<methodsynopsis>
12
12
<type>array</type><methodname>array_filter</methodname>
13
13
<methodparam><type>array</type><parameter>array</parameter></methodparam>
14
-
<methodparam choice="opt"><type>callable</type><parameter>callback</parameter></methodparam>
15
-
<methodparam choice="opt"><type>int</type><parameter>flag</parameter><initializer>0</initializer></methodparam>
14
+
<methodparam choice="opt"><type class="union"><type>callable</type><type>null</type></type><parameter>callback</parameter><initializer>&null;</initializer></methodparam>
15
+
<methodparam choice="opt"><type>int</type><parameter>mode</parameter><initializer>0</initializer></methodparam>
16
16
</methodsynopsis>
17
17
<para>
18
18
Iterates over each value in the <parameter>array</parameter>
19
19
passing them to the <parameter>callback</parameter> function.
20
-
If the <parameter>callback</parameter> function returns true, the
20
+
If the <parameter>callback</parameter> function returns &true;, the
21
21
current value from <parameter>array</parameter> is returned into
22
-
the result array. Array keys are preserved.
22
+
the result &array;.
23
+
</para>
24
+
<para>
25
+
Array keys are preserved, and may result in gaps if the <parameter>array</parameter> was indexed.
26
+
The result &array; can be reindexed using the <function>array_values</function> function.
23
27
</para>
24
28
</refsect1>
25
29

...
...
@@ -44,16 +48,15 @@
44
48
The callback function to use
45
49
</para>
46
50
<para>
47
-
If no <parameter>callback</parameter> is supplied, all entries of
48
-
<parameter>array</parameter> equal to &false; (see
49
-
<link linkend="language.types.boolean.casting">converting to
50
-
boolean</link>) will be removed.
51
+
If no <parameter>callback</parameter> is supplied, all empty entries of
52
+
<parameter>array</parameter> will be removed. See <function>empty</function>
53
+
for how PHP defines empty in this case.
51
54
</para>
52
55
</listitem>
53
56
</varlistentry>
54
57

55
58
<varlistentry>
56
-
<term><parameter>flag</parameter></term>
59
+
<term><parameter>mode</parameter></term>
57
60
<listitem>
58
61
<para>
59
62
Flag determining what arguments are sent to <parameter>callback</parameter>:
...
...
@@ -67,6 +70,8 @@
67
70
arguments to <parameter>callback</parameter> instead of the value</simpara>
68
71
</listitem>
69
72
</itemizedlist>
73
+
Default is <literal>0</literal> which will pass value as the only argument
74
+
to <parameter>callback</parameter> instead.
70
75
</para>
71
76
</listitem>
72
77
</varlistentry>
...
...
@@ -94,13 +99,12 @@
94
99
</thead>
95
100
<tbody>
96
101
<row>
97
-
<entry>5.6.0</entry>
102
+
<entry>8.0.0</entry>
98
103
<entry>
99
-
Added optional <parameter>flag</parameter> parameter and constants
100
-
<constant>ARRAY_FILTER_USE_KEY</constant> and
101
-
<constant>ARRAY_FILTER_USE_BOTH</constant>
104
+
<parameter>callback</parameter> is nullable now.
102
105
</entry>
103
106
</row>
107
+
&array.changelog.by-ref;
104
108
</tbody>
105
109
</tgroup>
106
110
</informaltable>
...
...
@@ -124,17 +128,17 @@
124
128
function odd($var)
125
129
{
126
130
// returns whether the input integer is odd
127
-
return($var & 1);
131
+
return $var & 1;
128
132
}
129
133

130
134
function even($var)
131
135
{
132
136
// returns whether the input integer is even
133
-
return(!($var & 1));
137
+
return !($var & 1);
134
138
}
135
139

136
-
$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
137
-
$array2 = array(6, 7, 8, 9, 10, 11, 12);
140
+
$array1 = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5];
141
+
$array2 = [6, 7, 8, 9, 10, 11, 12];
138
142

139
143
echo "Odd :\n";
140
144
print_r(array_filter($array1, "odd"));
...
...
@@ -171,13 +175,15 @@ Array
171
175
<![CDATA[
172
176
<?php
173
177

174
-
$entry = array(
175
-
0 => 'foo',
176
-
1 => false,
177
-
2 => -1,
178
-
3 => null,
179
-
4 => ''
180
-
);
178
+
$entry = [
179
+
0 => 'foo',
180
+
1 => false,
181
+
2 => -1,
182
+
3 => null,
183
+
4 => '',
184
+
5 => '0',
185
+
6 => 0,
186
+
];
181
187

182
188
print_r(array_filter($entry));
183
189
?>
...
...
@@ -196,7 +202,7 @@ Array
196
202
</example>
197
203
<example>
198
204
<title><function>array_filter</function> with
199
-
<parameter>flag</parameter></title>
205
+
<parameter>mode</parameter></title>
200
206
<programlisting role="php">
201
207
<![CDATA[
202
208
<?php
...
...
@@ -246,6 +252,7 @@ array(2) {
246
252
&reftitle.seealso;
247
253
<para>
248
254
<simplelist>
255
+
<member><function>array_intersect</function></member>
249
256
<member><function>array_map</function></member>
250
257
<member><function>array_reduce</function></member>
251
258
<member><function>array_walk</function></member>
...
...
@@ -254,7 +261,6 @@ array(2) {
254
261
</refsect1>
255
262

256
263
</refentry>
257
-

258
264
<!-- Keep this comment at the end of the file
259
265
Local variables:
260
266
mode: sgml
261
267