In PHP, filter_input_array() reads several variables from GET, POST, cookies, or the server at once and filters each one with its own rule.
Think of it as filter_input() for a whole form. You give it a list of field names and the filter for each, and it returns an array with the cleaned values.
Syntax
Call filter_input_array() with the values you need. Start with this line.
Leaves out any field in the definition that was not sent.
The second argument can be a single filter ID applied to every variable, or an array that maps each field name to a filter or to an array with 'filter', 'flags', and 'options' keys.
The definition lists two fields we would like to read.
add_empty is false, so missing fields are not added with a null value.
Nothing was sent, so the result has no keys.
count() prints 0. With the default true, the keys would exist with null values.
Example 3 — Per-Field Options with a Range
Use an array for a field to add options, such as a minimum and maximum age, and a sanitize filter for the email.
PHP
<?php
$definition = [
'email' => FILTER_SANITIZE_EMAIL,
'age' => [
'filter' => FILTER_VALIDATE_INT,
'options' => ['min_range' => 18, 'max_range' => 65],
],
];
$data = filter_input_array(INPUT_POST, $definition);
if (!is_array($data) || $data['age'] === null) {
echo "No form data to process.", PHP_EOL;
} elseif ($data['age'] === false) {
echo "Age must be between 18 and 65.", PHP_EOL;
} else {
echo "Welcome, ", $data['email'], PHP_EOL;
}
Output
No form data to process.
How It Works
FILTER_SANITIZE_EMAIL removes characters that cannot appear in an email address.
The age field uses an array with filter and options keys to set a range.
The script first checks that an array came back and that age was sent.
From the command line nothing is posted, so the first message prints. A real form would reach the other branches.
Frequently Asked Questions
It reads multiple variables from GET, POST, cookies, or the server and applies a filter to each one. It returns an array with the filtered values.
filter_input_array() reads from the real request source, like INPUT_POST. filter_var_array() filters an array you already have. The rules for the filter definitions are the same.
When it is true, fields in your definition that were not sent are still added to the result with the value null. When it is false, those fields are left out.
Yes. Pass a single filter ID, such as FILTER_DEFAULT or FILTER_SANITIZE_SPECIAL_CHARS, as the second argument. It will be applied to every variable in that input source.
It returns null when the input source is not set, for example when no GET or POST data was received. Use the null coalescing operator, like ?? [], to handle that safely.