PHP filter_input_array() Function

Definition and Usage

The filter_input_array() function retrieves multiple inputs from outside the script and filters them.

This function does not need to call filter_input() repeatedly, which is very useful for filtering multiple input variables.

This function can obtain input from various sources:

  • INPUT_GET
  • INPUT_POST
  • INPUT_COOKIE
  • INPUT_ENV
  • INPUT_SERVER
  • INPUT_SESSION (Not yet implemented)
  • INPUT_REQUEST (Not yet implemented)

If successful, returns filtered data; if failed, returns false.

Syntax

filter_input(input_type, args)
Parameter Description
input_type Mandatory. Specifies the input type. See the list above for possible types.
args

Optional. Specifies an array of filter parameters.

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

The 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 Manual, see the filters that can be used with this function.

Example

In this example, we use the filter_input_array() function to filter three POST variables. The accepted POST variables are name, age, and email address:

<?php
$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_input_array(INPUT_POST, $filters));
?>

Output similar to:

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