PHP Function Reference

PHP filter_input_array() Function



The PHP filter_input_array() function gets external variables and optionally filters them. This function is useful for retrieving many values without repetitively calling filter_input().

Syntax

filter_input_array(type, options, add_empty)

Parameters

type Required. Specify the input type. It can be one of the following:
  • INPUT_GET
  • INPUT_POST
  • INPUT_COOKIE
  • INPUT_SERVER
  • INPUT_ENV
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. If the input array designated by type is not populated, the function returns null if the FILTER_NULL_ON_FAILURE flag is not given, or false otherwise. For other failures, false is returned.

An array value will be false if the filter fails, or null if the variable is not set. Or if the flag FILTER_NULL_ON_FAILURE is used, it returns false if the variable is not set and null if the filter fails. If the add_empty parameter is false, no array element will be added for unset variables.

Example: filtering POST variables

In the example below, filter_input_array() function is used to filter three POST variables. The received POST variables are 'name', 'age' and 'email'.

<?php
/*considering this data coming from POST
$_POST = 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_input_array(INPUT_POST, $filters));
?>

The output of the above code will be:

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

Example: filter_input_array() example

Consider one more example where this function is used to validate and sanitize the data coming from POST method.

<?php
/*considering this data coming from POST
$_POST = 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_input_array(INPUT_POST, $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