The filter_input_array() function takes multiple inputs (such as form inputs) from outside the script and filters them.
This function is useful for filtering multiple input variables without repeatedly calling filter_input().
This function can take 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, the filtered data is returned as an array. If it fails, returns FALSE.
filter_input_array(input_type, filter_args)
parameter | describe |
---|---|
input_type | Required. Specifies the input type. See the list of possible types above. |
filter_args | Optional. Specifies an array of filter parameters. Legal array keys are variable names, and legal 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. The filter ID can be an ID name (such as FILTER_VALIDATE_EMAIL) or an ID number (such as 274). |
Tip: See the complete PHP Filter reference manual to see the filters that can be used with this function.
In this example, we use the filter_input_array() function to filter three POST variables. The POST variables received are name, age, and 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 looks like this:
Array ( [name] => Peter [age] => 41 [email] => [email protected] )