PHP Function Reference

PHP filter_var_array() Function



The PHP filter_var_array() function gets multiple variables and optionally filters them. This function is useful for retrieving many values without repetitively calling filter_var().

Syntax

filter_var_array(array, options, add_empty)

Parameters

value Required. Specify an array with string keys containing the data to filter.
options Optional. Specify an array of filter arguments. A valid array key is a variable name, and a valid value is a filter name or ID, or an array specifying the filter, flags and options. This parameter can also be a single filter name/ID; then all values in the input array are filtered by the specified filter. See the filter list.
add_empty Optional. If set to true, adds missing keys as null to the return value. Default is true.

Return Value

Returns 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.

Example: filtering variables

In the example below, filter_var_array() function is used to filter three variables. These three variables are 'name', 'age' and 'email'.

<?php
$data = array(
  'name'  => 'John Smith',
  'age'   =>  25,
  'email' => 'John@example.com',
);

$filters = array (
  "name"  => array ("filter"=>FILTER_CALLBACK,
                    "flags"=>FILTER_FORCE_ARRAY,
                    "options"=>"ucwords"
                   ),
  "age"   => array ("filter"=>FILTER_VALIDATE_INT,
                    "options"=>array("min_range"=>1,
                                     "max_range"=>100)
                   ),
  "email" => FILTER_VALIDATE_EMAIL
);

print_r(filter_var_array($data, $filters));
?>

The output of the above code will be:

Array
(
    [name] => John Smith
    [age] => 25
    [email] => John@example.com
)

Example: filter_var_array() example

Consider one more example where this function is used to validate and sanitize the data contained in an array.

<?php
$data = array(
  'product_id' => 'libgd<script>',
  'component'  => array('10'),
  'version'    => '2.0.33',
  'testarray'  => array('2', '23', '10', '12'),
  'testscalar' => '2',
);

$args = array(
  'product_id'  => FILTER_SANITIZE_ENCODED,
  'component'    => array('filter'  => FILTER_VALIDATE_INT,
                          'flags'   => FILTER_REQUIRE_ARRAY, 
                          'options' => array('min_range' => 1, 
                                            'max_range' => 10)
                         ),
  'version'      => FILTER_SANITIZE_ENCODED,
  'doesnotexist' => FILTER_VALIDATE_INT,
  'testscalar'   => array('filter' => FILTER_VALIDATE_INT,
                          'flags'  => FILTER_REQUIRE_SCALAR,
                         ),
  'testarray'    => array('filter' => FILTER_VALIDATE_INT,
                          'flags'  => FILTER_REQUIRE_ARRAY,
                         )
);

$myinputs = filter_var_array($data, $args);

var_dump($myinputs);
?>

The output of the above code will be:

array(6) {
  ["product_id"]=>
  string(17) "libgd%3Cscript%3E"
  ["component"]=>
  array(1) {
    [0]=>
    int(10)
  }
  ["version"]=>
  string(6) "2.0.33"
  ["doesnotexist"]=>
  NULL
  ["testscalar"]=>
  int(2)
  ["testarray"]=>
  array(4) {
    [0]=>
    int(2)
    [1]=>
    int(23)
    [2]=>
    int(10)
    [3]=>
    int(12)
  }
}

❮ PHP Filter Reference