1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.filter-var-array" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>filter_var_array</refname>
<refpurpose>Gets multiple variables and optionally filters them</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type class="union"><type>array</type><type>false</type><type>null</type></type><methodname>filter_var_array</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam choice="opt"><type class="union"><type>array</type><type>int</type></type><parameter>options</parameter><initializer><constant>FILTER_DEFAULT</constant></initializer></methodparam>
<methodparam choice="opt"><type>bool</type><parameter>add_empty</parameter><initializer>&true;</initializer></methodparam>
</methodsynopsis>
<simpara>
Filter an associative &array; of values using
<constant>FILTER_VALIDATE_<replaceable>*</replaceable></constant>
validation filters,
<constant>FILTER_SANITIZE_<replaceable>*</replaceable></constant>
sanitization filters, or custom filters.
</simpara>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term><parameter>array</parameter></term>
<listitem>
<simpara>
An associative &array; containing the data to filter.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>options</parameter></term>
<listitem>
<simpara>
Either an associative <type>array</type> of options,
or the filter to apply to each entry,
which can either be a validation filter by using one of the
<constant>FILTER_VALIDATE_<replaceable>*</replaceable></constant>
constants, or a sanitization filter by using one of the
<constant>FILTER_SANITIZE_<replaceable>*</replaceable></constant>
constants.
</simpara>
<simpara>
The option array is an associative array where the key corresponds
to a key in the data <parameter>array</parameter> and the associated
value is either the filter to apply to this entry,
or an associative array describing how and which filter should be
applied to this entry.
</simpara>
<simpara>
The associative array describing how a filter should be applied
must contain the <literal>'filter'</literal> key whose associated
value is the filter to apply, which can be one of the
<constant>FILTER_VALIDATE_<replaceable>*</replaceable></constant>,
<constant>FILTER_SANITIZE_<replaceable>*</replaceable></constant>,
<constant>FILTER_UNSAFE_RAW</constant>, or
<constant>FILTER_CALLBACK</constant> constants.
It can optionally contain the <literal>'flags'</literal> key
which specifies and flags that apply to the filter,
and the <literal>'options'</literal> key which specifies any options
that apply to the filter.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>add_empty</parameter></term>
<listitem>
<para>
Add missing keys as &null; to the return value.
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
An array containing the values of the requested variables on success, or &false;
on failure. An array value will be &false; if the filter fails, or &null; if
the variable is not set.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>A <function>filter_var_array</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
$data = [
'product_id' => 'libgd<script>',
'component' => '10',
'versions' => '2.0.33',
'testscalar' => ['2', '23', '10', '12'],
'testarray' => '2',
];
$filters = [
'product_id' => FILTER_SANITIZE_ENCODED,
'component' => [
'filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_FORCE_ARRAY,
'options' => [
'min_range' => 1,
'max_range' => 10,
],
],
'versions' => [
'filter' => FILTER_SANITIZE_ENCODED
],
'testscalar' => [
'filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_REQUIRE_SCALAR,
],
'testarray' => [
'filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_FORCE_ARRAY,
],
'doesnotexist' => FILTER_VALIDATE_INT,
];
var_dump(filter_var_array($data, $filters));
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
array(6) {
["product_id"]=>
string(17) "libgd%3Cscript%3E"
["component"]=>
array(1) {
[0]=>
int(10)
}
["versions"]=>
string(6) "2.0.33"
["testscalar"]=>
bool(false)
["testarray"]=>
array(1) {
[0]=>
int(2)
}
["doesnotexist"]=>
NULL
}
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
<member><function>filter_input_array</function></member>
<member><function>filter_var</function></member>
<member><function>filter_input</function></member>
<member>
Validation filters
<constant>FILTER_VALIDATE_<replaceable>*</replaceable></constant>
</member>
<member>
Sanitization filters
<constant>FILTER_SANITIZE_<replaceable>*</replaceable></constant>
</member>
</simplelist>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|