reference/filter/functions/filter-var-array.xml
53054bf8decc8648cf2e90a493692a161e2371af
53054bf8decc8648cf2e90a493692a161e2371af
...
...
@@ -5,54 +5,80 @@
5
5
<refname>filter_var_array</refname>
6
6
<refpurpose>Gets multiple variables and optionally filters them</refpurpose>
7
7
</refnamediv>
8
+
8
9
<refsect1 role="description">
9
10
&reftitle.description;
10
11
<methodsynopsis>
11
-
<type>mixed</type><methodname>filter_var_array</methodname>
12
-
<methodparam><type>array</type><parameter>data</parameter></methodparam>
13
-
<methodparam choice="opt"><type>mixed</type><parameter>definition</parameter></methodparam>
12
+
<type class="union"><type>array</type><type>false</type><type>null</type></type><methodname>filter_var_array</methodname>
13
+
<methodparam><type>array</type><parameter>array</parameter></methodparam>
14
+
<methodparam choice="opt"><type class="union"><type>array</type><type>int</type></type><parameter>options</parameter><initializer><constant>FILTER_DEFAULT</constant></initializer></methodparam>
15
+
<methodparam choice="opt"><type>bool</type><parameter>add_empty</parameter><initializer>&true;</initializer></methodparam>
14
16
</methodsynopsis>
15
-
<para>
16
-
This function is useful for retrieving many values without
17
-
repetitively calling <function>filter_var</function>.
18
-
</para>
17
+
<simpara>
18
+
Filter an associative &array; of values using
19
+
<constant>FILTER_VALIDATE_<replaceable>*</replaceable></constant>
20
+
validation filters,
21
+
<constant>FILTER_SANITIZE_<replaceable>*</replaceable></constant>
22
+
sanitization filters, or custom filters.
23
+
</simpara>
19
24
</refsect1>
25
+
20
26
<refsect1 role="parameters">
21
27
&reftitle.parameters;
22
-
<para>
23
-
<variablelist>
24
-
<varlistentry>
25
-
<term><parameter>data</parameter></term>
26
-
<listitem>
27
-
<para>
28
-
An array with string keys containing the data to filter.
29
-
</para>
30
-
</listitem>
31
-
</varlistentry>
32
-
<varlistentry>
33
-
<term><parameter>definition</parameter></term>
34
-
<listitem>
35
-
<para>
36
-
An array defining the arguments. A valid key is a <type>string</type>
37
-
containing a variable name and a valid value is either a filter type,
38
-
or an <type>array</type> optionally specifying the filter, flags and
39
-
options. If the value is an array, valid keys are
40
-
<literal>filter</literal> which specifies the filter type,
41
-
<literal>flags</literal> which specifies any flags that apply to the
42
-
filter, and <literal>options</literal> which specifies any options
43
-
that apply to the filter. See the example below for a better
44
-
understanding.
45
-
</para>
46
-
<para>
47
-
This parameter can be also an integer holding a <link
48
-
linkend="filter.constants">filter constant</link>. Then all values in the
49
-
input array are filtered by this filter.
50
-
</para>
51
-
</listitem>
52
-
</varlistentry>
53
-
</variablelist>
54
-
</para>
28
+
<variablelist>
29
+
<varlistentry>
30
+
<term><parameter>array</parameter></term>
31
+
<listitem>
32
+
<simpara>
33
+
An associative &array; containing the data to filter.
34
+
</simpara>
35
+
</listitem>
36
+
</varlistentry>
37
+
<varlistentry>
38
+
<term><parameter>options</parameter></term>
39
+
<listitem>
40
+
<simpara>
41
+
Either an associative <type>array</type> of options,
42
+
or the filter to apply to each entry,
43
+
which can either be a validation filter by using one of the
44
+
<constant>FILTER_VALIDATE_<replaceable>*</replaceable></constant>
45
+
constants, or a sanitization filter by using one of the
46
+
<constant>FILTER_SANITIZE_<replaceable>*</replaceable></constant>
47
+
constants.
48
+
</simpara>
49
+
<simpara>
50
+
The option array is an associative array where the key corresponds
51
+
to a key in the data <parameter>array</parameter> and the associated
52
+
value is either the filter to apply to this entry,
53
+
or an associative array describing how and which filter should be
54
+
applied to this entry.
55
+
</simpara>
56
+
<simpara>
57
+
The associative array describing how a filter should be applied
58
+
must contain the <literal>'filter'</literal> key whose associated
59
+
value is the filter to apply, which can be one of the
60
+
<constant>FILTER_VALIDATE_<replaceable>*</replaceable></constant>,
61
+
<constant>FILTER_SANITIZE_<replaceable>*</replaceable></constant>,
62
+
<constant>FILTER_UNSAFE_RAW</constant>, or
63
+
<constant>FILTER_CALLBACK</constant> constants.
64
+
It can optionally contain the <literal>'flags'</literal> key
65
+
which specifies and flags that apply to the filter,
66
+
and the <literal>'options'</literal> key which specifies any options
67
+
that apply to the filter.
68
+
</simpara>
69
+
</listitem>
70
+
</varlistentry>
71
+
<varlistentry>
72
+
<term><parameter>add_empty</parameter></term>
73
+
<listitem>
74
+
<para>
75
+
Add missing keys as &null; to the return value.
76
+
</para>
77
+
</listitem>
78
+
</varlistentry>
79
+
</variablelist>
55
80
</refsect1>
81
+
56
82
<refsect1 role="returnvalues">
57
83
&reftitle.returnvalues;
58
84
<para>
...
...
@@ -70,38 +96,41 @@
70
96
<programlisting role="php">
71
97
<![CDATA[
72
98
<?php
73
-
error_reporting(E_ALL | E_STRICT);
74
-
$data = array(
75
-
'product_id' => 'libgd<script>',
76
-
'component' => '10',
77
-
'versions' => '2.0.33',
78
-
'testscalar' => array('2', '23', '10', '12'),
79
-
'testarray' => '2',
80
-
);
81
99
82
-
$args = array(
100
+
$data = [
101
+
'product_id' => 'libgd<script>',
102
+
'component' => '10',
103
+
'versions' => '2.0.33',
104
+
'testscalar' => ['2', '23', '10', '12'],
105
+
'testarray' => '2',
106
+
];
107
+
108
+
$filters = [
83
109
'product_id' => FILTER_SANITIZE_ENCODED,
84
-
'component' => array('filter' => FILTER_VALIDATE_INT,
85
-
'flags' => FILTER_FORCE_ARRAY,
86
-
'options' => array('min_range' => 1, 'max_range' => 10)
87
-
),
88
-
'versions' => FILTER_SANITIZE_ENCODED,
110
+
'component' => [
111
+
'filter' => FILTER_VALIDATE_INT,
112
+
'flags' => FILTER_FORCE_ARRAY,
113
+
'options' => [
114
+
'min_range' => 1,
115
+
'max_range' => 10,
116
+
],
117
+
],
118
+
'versions' => [
119
+
'filter' => FILTER_SANITIZE_ENCODED
120
+
],
121
+
'testscalar' => [
122
+
'filter' => FILTER_VALIDATE_INT,
123
+
'flags' => FILTER_REQUIRE_SCALAR,
124
+
],
125
+
'testarray' => [
126
+
'filter' => FILTER_VALIDATE_INT,
127
+
'flags' => FILTER_FORCE_ARRAY,
128
+
],
89
129
'doesnotexist' => FILTER_VALIDATE_INT,
90
-
'testscalar' => array(
91
-
'filter' => FILTER_VALIDATE_INT,
92
-
'flags' => FILTER_REQUIRE_SCALAR,
93
-
),
94
-
'testarray' => array(
95
-
'filter' => FILTER_VALIDATE_INT,
96
-
'flags' => FILTER_FORCE_ARRAY,
97
-
)
98
-
99
-
);
130
+
];
100
131
101
-
$myinputs = filter_var_array($data, $args);
132
+
var_dump(filter_var_array($data, $filters));
102
133
103
-
var_dump($myinputs);
104
-
echo "\n";
105
134
?>
106
135
]]>
107
136
</programlisting>
...
...
@@ -110,22 +139,14 @@ echo "\n";
110
139
<![CDATA[
111
140
array(6) {
112
141
["product_id"]=>
113
-
array(1) {
114
-
[0]=>
115
-
string(17) "libgd%3Cscript%3E"
116
-
}
142
+
string(17) "libgd%3Cscript%3E"
117
143
["component"]=>
118
144
array(1) {
119
145
[0]=>
120
146
int(10)
121
147
}
122
148
["versions"]=>
123
-
array(1) {
124
-
[0]=>
125
-
string(6) "2.0.33"
126
-
}
127
-
["doesnotexist"]=>
128
-
NULL
149
+
string(6) "2.0.33"
129
150
["testscalar"]=>
130
151
bool(false)
131
152
["testarray"]=>
...
...
@@ -133,6 +154,8 @@ array(6) {
133
154
[0]=>
134
155
int(2)
135
156
}
157
+
["doesnotexist"]=>
158
+
NULL
136
159
}
137
160
]]>
138
161
</screen>
...
...
@@ -142,17 +165,22 @@ array(6) {
142
165
143
166
<refsect1 role="seealso">
144
167
&reftitle.seealso;
145
-
<para>
146
-
<simplelist>
147
-
<member><function>filter_input_array</function></member>
148
-
<member><function>filter_var</function></member>
149
-
<member><function>filter_input</function></member>
150
-
</simplelist>
151
-
</para>
168
+
<simplelist>
169
+
<member><function>filter_input_array</function></member>
170
+
<member><function>filter_var</function></member>
171
+
<member><function>filter_input</function></member>
172
+
<member>
173
+
Validation filters
174
+
<constant>FILTER_VALIDATE_<replaceable>*</replaceable></constant>
175
+
</member>
176
+
<member>
177
+
Sanitization filters
178
+
<constant>FILTER_SANITIZE_<replaceable>*</replaceable></constant>
179
+
</member>
180
+
</simplelist>
152
181
</refsect1>
153
182
154
183
</refentry>
155
-
156
184
<!-- Keep this comment at the end of the file
157
185
Local variables:
158
186
mode: sgml
159
187