<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.filter.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.filter-var-array.php',
    1 => 'filter_var_array',
    2 => 'Gets multiple variables and optionally filters them',
  ),
  'up' => 
  array (
    0 => 'ref.filter.php',
    1 => 'Filter Functions',
  ),
  'prev' => 
  array (
    0 => 'function.filter-var.php',
    1 => 'filter_var',
  ),
  'next' => 
  array (
    0 => 'class.filter-filterexception.php',
    1 => 'Filter\\FilterException',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/filter/functions/filter-var-array.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.filter-var-array" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">filter_var_array</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.2.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">filter_var_array</span> &mdash; <span class="dc-title">Gets multiple variables and optionally filters them</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.filter-var-array-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">function</span> <span class="methodname"><strong>filter_var_array</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$array</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.integer.php" class="type int">int</a></span></span> <code class="parameter">$options</code><span class="initializer"> = <strong><code><a href="filter.constants.sanitization.php#constant.filter-default">FILTER_DEFAULT</a></code></strong></span></span>, <span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$add_empty</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></span></span>): <span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span>|<span class="type"><a href="language.types.null.php" class="type null">null</a></span></span></div>

  <p class="simpara">
   Filter an associative <span class="type"><a href="language.types.array.php" class="type array">array</a></span> of values using
   <strong><code><a href="filter.constants.validation.php#constant.filter-validate-bool">FILTER_VALIDATE_<span class="replaceable">*</span></a></code></strong>
   validation filters,
   <strong><code><a href="filter.constants.sanitization.php#constant.filter-sanitize-string">FILTER_SANITIZE_<span class="replaceable">*</span></a></code></strong>
   sanitization filters, or custom filters.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.filter-var-array-parameters">
  <h3 class="title">Parameters</h3>
  <dl>
   
    <dt><code class="parameter">array</code></dt>
    <dd>
     <p class="simpara">
      An associative <span class="type"><a href="language.types.array.php" class="type array">array</a></span> containing the data to filter.
     </p>
    </dd>
   
   
    <dt><code class="parameter">options</code></dt>
    <dd>
     <p class="simpara">
      Either an associative <span class="type"><a href="language.types.array.php" class="type array">array</a></span> of options,
      or the filter to apply to each entry,
      which can either be a validation filter by using one of the
      <strong><code><a href="filter.constants.validation.php#constant.filter-validate-bool">FILTER_VALIDATE_<span class="replaceable">*</span></a></code></strong>
      constants, or a sanitization filter by using one of the
      <strong><code><a href="filter.constants.sanitization.php#constant.filter-sanitize-string">FILTER_SANITIZE_<span class="replaceable">*</span></a></code></strong>
      constants.
     </p>
     <p class="simpara">
      The option array is an associative array where the key corresponds
      to a key in the input array 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.
     </p>
     <p class="simpara">
      The associative array describing how a filter should be applied
      must contain the <code class="literal">&#039;filter&#039;</code> key whose associated
      value is the filter to apply, which can be one of the
      <strong><code><a href="filter.constants.validation.php#constant.filter-validate-bool">FILTER_VALIDATE_<span class="replaceable">*</span></a></code></strong>,
      <strong><code><a href="filter.constants.sanitization.php#constant.filter-sanitize-string">FILTER_SANITIZE_<span class="replaceable">*</span></a></code></strong>,
      <strong><code><a href="filter.constants.sanitization.php#constant.filter-unsafe-raw">FILTER_UNSAFE_RAW</a></code></strong>, or
      <strong><code><a href="filter.constants.callback.php#constant.filter-callback">FILTER_CALLBACK</a></code></strong> constants.
      It can optionally contain the <code class="literal">&#039;flags&#039;</code> key
      which specifies any flags that apply to the filter,
      and the <code class="literal">&#039;options&#039;</code> key which specifies any options
      that apply to the filter.
     </p>
    </dd>
   
   
    <dt><code class="parameter">add_empty</code></dt>
    <dd>
     <p class="para">
      Add missing keys as <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> to the return value.
     </p>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.filter-var-array-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="simpara">
   On success, an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> containing the values of the requested variables.
  </p>
  <p class="simpara">
   On failure, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> is returned.
  </p>
  <p class="simpara">
   Missing entries from the input array are added to the returned <span class="type"><a href="language.types.array.php" class="type array">array</a></span> as
   <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> if <code class="parameter">add_empty</code> is <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>,
   and are omitted entirely if it is <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
  </p>
  <p class="simpara">
   An entry of the returned <span class="type"><a href="language.types.array.php" class="type array">array</a></span> will be <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> if the filter fails,
   unless the <strong><code><a href="filter.constants.flags.generic.php#constant.filter-null-on-failure">FILTER_NULL_ON_FAILURE</a></code></strong> flag is used,
   in which case it will be <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
   With the <strong><code><a href="filter.constants.flags.generic.php#constant.filter-force-array">FILTER_FORCE_ARRAY</a></code></strong> flag,
   that failure value is wrapped in a one element <span class="type"><a href="language.types.array.php" class="type array">array</a></span>
   like any other result.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.filter-var-array-examples">
  <h3 class="title">Examples</h3>
  <div class="example" id="example-1">
   <p><strong>Example #1 A <span class="function"><strong>filter_var_array()</strong></span> example</strong></p>
   <div class="example-contents"><p>
    Entries are filtered as scalars unless
    <strong><code><a href="filter.constants.flags.generic.php#constant.filter-require-array">FILTER_REQUIRE_ARRAY</a></code></strong> or
    <strong><code><a href="filter.constants.flags.generic.php#constant.filter-force-array">FILTER_FORCE_ARRAY</a></code></strong> is used.
    The <strong><code><a href="filter.constants.flags.generic.php#constant.filter-require-scalar">FILTER_REQUIRE_SCALAR</a></code></strong> flag on
    <code class="literal">testscalar</code> below therefore only states that default
    explicitly.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php

$data </span><span style="color: #007700">= [
    </span><span style="color: #DD0000">'product_id' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'libgd&lt;script&gt;'</span><span style="color: #007700">,
    </span><span style="color: #DD0000">'component'  </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'10'</span><span style="color: #007700">,
    </span><span style="color: #DD0000">'versions'   </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'2.0.33'</span><span style="color: #007700">,
    </span><span style="color: #DD0000">'testscalar' </span><span style="color: #007700">=&gt; [</span><span style="color: #DD0000">'2'</span><span style="color: #007700">, </span><span style="color: #DD0000">'23'</span><span style="color: #007700">, </span><span style="color: #DD0000">'10'</span><span style="color: #007700">, </span><span style="color: #DD0000">'12'</span><span style="color: #007700">],
    </span><span style="color: #DD0000">'testarray'  </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'2'</span><span style="color: #007700">,
];

</span><span style="color: #0000BB">$filters </span><span style="color: #007700">= [
    </span><span style="color: #DD0000">'product_id'   </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_SANITIZE_ENCODED</span><span style="color: #007700">,
    </span><span style="color: #DD0000">'component'    </span><span style="color: #007700">=&gt; [
        </span><span style="color: #DD0000">'filter'   </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'flags'    </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_FORCE_ARRAY</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'options'  </span><span style="color: #007700">=&gt; [
            </span><span style="color: #DD0000">'min_range' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">1</span><span style="color: #007700">,
            </span><span style="color: #DD0000">'max_range' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">10</span><span style="color: #007700">,
        ],
    ],
    </span><span style="color: #DD0000">'versions'     </span><span style="color: #007700">=&gt; [
        </span><span style="color: #DD0000">'filter' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_SANITIZE_ENCODED
    </span><span style="color: #007700">],
    </span><span style="color: #DD0000">'testscalar'   </span><span style="color: #007700">=&gt; [
        </span><span style="color: #DD0000">'filter' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'flags'  </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_REQUIRE_SCALAR</span><span style="color: #007700">,
    ],
    </span><span style="color: #DD0000">'testarray'    </span><span style="color: #007700">=&gt; [
        </span><span style="color: #DD0000">'filter' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'flags'  </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_FORCE_ARRAY</span><span style="color: #007700">,
    ],
    </span><span style="color: #DD0000">'doesnotexist' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,
];

</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">filter_var_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">, </span><span style="color: #0000BB">$filters</span><span style="color: #007700">));

</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
   </div>

   <div class="example-contents"><p>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">array(6) {
  [&quot;product_id&quot;]=&gt;
  string(17) &quot;libgd%3Cscript%3E&quot;
  [&quot;component&quot;]=&gt;
  array(1) {
    [0]=&gt;
    int(10)
  }
  [&quot;versions&quot;]=&gt;
  string(6) &quot;2.0.33&quot;
  [&quot;testscalar&quot;]=&gt;
  bool(false)
  [&quot;testarray&quot;]=&gt;
  array(1) {
    [0]=&gt;
    int(2)
  }
  [&quot;doesnotexist&quot;]=&gt;
  NULL
}</pre>
</div>
   </div>
  </div>
  <div class="example" id="example-2">
   <p><strong>Example #2 Applying a single filter to all values</strong></p>
   <div class="example-contents"><p>
    When <code class="parameter">options</code> is an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>, the same filter
    is applied to every entry in the array.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
$data </span><span style="color: #007700">= [
    </span><span style="color: #DD0000">'name'  </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'&lt;b&gt;John&lt;/b&gt;'</span><span style="color: #007700">,
    </span><span style="color: #DD0000">'email' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'john@example&lt;script&gt;.com'</span><span style="color: #007700">,
    </span><span style="color: #DD0000">'bio'   </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Developer &amp; writer'</span><span style="color: #007700">,
];

</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">filter_var_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">, </span><span style="color: #0000BB">FILTER_SANITIZE_SPECIAL_CHARS</span><span style="color: #007700">));
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
   </div>

   <div class="example-contents"><p>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">array(3) {
  [&quot;name&quot;]=&gt;
  string(27) &quot;&amp;#60;b&amp;#62;John&amp;#60;/b&amp;#62;&quot;
  [&quot;email&quot;]=&gt;
  string(32) &quot;john@example&amp;#60;script&amp;#62;.com&quot;
  [&quot;bio&quot;]=&gt;
  string(22) &quot;Developer &amp;#38; writer&quot;
}</pre>
</div>
   </div>
  </div>
  <div class="example" id="example-3">
   <p><strong>Example #3 Using <strong><code><a href="filter.constants.callback.php#constant.filter-callback">FILTER_CALLBACK</a></code></strong></strong></p>
   <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
$data </span><span style="color: #007700">= [
    </span><span style="color: #DD0000">'name'  </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'  John Doe  '</span><span style="color: #007700">,
    </span><span style="color: #DD0000">'city'  </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'  New York  '</span><span style="color: #007700">,
];

</span><span style="color: #0000BB">$options </span><span style="color: #007700">= [
    </span><span style="color: #DD0000">'name' </span><span style="color: #007700">=&gt; [
        </span><span style="color: #DD0000">'filter'  </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_CALLBACK</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'options' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'trim'</span><span style="color: #007700">,
    ],
    </span><span style="color: #DD0000">'city' </span><span style="color: #007700">=&gt; [
        </span><span style="color: #DD0000">'filter'  </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_CALLBACK</span><span style="color: #007700">,
        </span><span style="color: #DD0000">'options' </span><span style="color: #007700">=&gt; function (</span><span style="color: #0000BB">$value</span><span style="color: #007700">) {
            return </span><span style="color: #0000BB">strtoupper</span><span style="color: #007700">(</span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">));
        },
    ],
];

</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">filter_var_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">, </span><span style="color: #0000BB">$options</span><span style="color: #007700">));
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
   </div>

   <div class="example-contents"><p>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">array(2) {
  [&quot;name&quot;]=&gt;
  string(8) &quot;John Doe&quot;
  [&quot;city&quot;]=&gt;
  string(8) &quot;NEW YORK&quot;
}</pre>
</div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.filter-var-array-seealso">
  <h3 class="title">See Also</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="function.filter-input-array.php" class="function" rel="rdfs-seeAlso">filter_input_array()</a> - Gets external variables and optionally filters them</span></li>
   <li><span class="function"><a href="function.filter-var.php" class="function" rel="rdfs-seeAlso">filter_var()</a> - Filters a variable with a specified filter</span></li>
   <li><span class="function"><a href="function.filter-input.php" class="function" rel="rdfs-seeAlso">filter_input()</a> - Gets a specific external variable by name and optionally filters it</span></li>
   <li>
    Validation filters
    <strong><code><a href="filter.constants.validation.php#constant.filter-validate-bool">FILTER_VALIDATE_<span class="replaceable">*</span></a></code></strong>
   </li>
   <li>
    Sanitization filters
    <strong><code><a href="filter.constants.sanitization.php#constant.filter-sanitize-string">FILTER_SANITIZE_<span class="replaceable">*</span></a></code></strong>
   </li>
  </ul>
 </div>


</div><?php manual_footer($setup); ?>