PHP filter_var_array() Function

Definition and Usage

The filter_var_array() function retrieves multiple variables and filters them.

Since it is not necessary to call filter_input() repeatedly, this function is very useful for filtering multiple variables.

If successful, returns an array containing the filtered variable values, if failed, returns false.

Syntax

filter_var_array(array, args)
Parameter Description
array Mandatory. Defines an array with string keys that contains the data to be filtered.
args

Optional. Specify an array of filter parameters.

Valid array keys are variable names. Valid values are filter IDs, or arrays specifying filters, flags, and options.

This parameter can also be a single filter ID. If so, all values in the input array are filtered by the specified filter.

Tips and Comments

Tip:See alsoComplete PHP Filter Reference ManualView the filters that can be used with this function.

Example

<?php
$arr = array
 (
 "name" => "peter griffin",
 "age" => "41",
 "email" => "peter@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"=>120
   )
  ),
 "email"=> FILTER_VALIDATE_EMAIL,
 );
print_r(filter_var_array($arr, $filters));
?>

Output similar to:

Array
 (
 [name] => Peter Griffin
 [age] => 41
 [email] => peter@example.com
 )