THE WORLD'S LARGEST WEB DEVELOPER SITE

PHP filter_input_array() Function

❮ PHP Filter Reference


Definition and Usage

The filter_input_array() function gets external variables (e.g. from form input) and optionally filters them.

This function is useful for retrieving/filtering many values instead of calling filter_input() many times.


Syntax

filter_input_array(type, definition, add_empty)
Parameter Description
type Required. The input type to check for. Can be one of the following:
  • INPUT_GET
  • INPUT_POST
  • INPUT_COOKIE
  • INPUT_SERVER
  • INPUT_ENV
definition Optional. Specifies 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
add_empty Optional. A Boolean value. When set to TRUE it add missing keys as NULL to the return value. Default value is TRUE


Technical Details

Return Value:

Returns an array containing the values of the variables on success, or FALSE on failure

PHP Version: 5.2.0+

Example

In this example we use the filter_input_array() function to filter three POST variables. The received POST variables is a name, an age and an e-mail 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));
?>

The output of the code should be:

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

❮ Complete PHP Filter Reference