PHP filter_input_array() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filter

Definition

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.

PHP
$data = filter_input_array(INPUT_POST, $definition);
CallWhat it does
filter_input_array(INPUT_POST, ['email' => FILTER_VALIDATE_EMAIL, 'age' => FILTER_VALIDATE_INT])Returns an array with email and age filtered by their own rules.
filter_input_array(INPUT_GET, FILTER_DEFAULT)Applies one filter to every GET variable and returns them all.
filter_input_array(INPUT_POST, $definition, false)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.

Library declarations
PHP
filter_input_array(int $type, array|int $options = FILTER_DEFAULT, bool $add_empty = true): array|false|null

Parameters

ParameterDescription
typeRequired. The input source: INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV.
optionsOptional. A filter ID applied to all values, or an array that defines the filter for each field. The default is FILTER_DEFAULT.
add_emptyOptional. If true, fields in the definition that were not sent are added with a null value. If false, they are left out. The default is true.

Return Value

Return ValueDescription
arrayAn array of the filtered values, keyed by field name. A failed filter gives false for that key, and a missing field gives null when add_empty is true.
falseReturned when the input type is invalid or a flag such as FILTER_NULL_ON_FAILURE changes the failure result.
nullReturned when the input source is not set.

PHP Examples

Example 1 — Filter Several GET Fields

Define one filter per field and read them together. From the command line no fields are sent, so each one is missing.

PHP
<?php
$data = filter_input_array(INPUT_GET, [
    'email' => FILTER_VALIDATE_EMAIL,
    'age'   => FILTER_VALIDATE_INT,
]) ?? [];

echo "Email: ", $data['email'] ?? 'missing', PHP_EOL;
echo "Age: ", $data['age'] ?? 'missing', PHP_EOL;

How It Works

  1. The array tells PHP which fields to read and which filter to use for each.
  2. filter_input_array() returns the filtered values for email and age.
  3. The ?? [] part turns a null result into an empty array so the next lines never fail.
  4. Each missing field prints the word missing.

Example 2 — Skip Missing Fields with add_empty

Set the third argument to false so fields that were not sent do not appear in the result at all.

PHP
<?php
$definition = [
    'email' => FILTER_VALIDATE_EMAIL,
    'age'   => FILTER_VALIDATE_INT,
];

$data = filter_input_array(INPUT_GET, $definition, false) ?? [];

echo "Fields received: ", count($data), PHP_EOL;

How It Works

  1. The definition lists two fields we would like to read.
  2. add_empty is false, so missing fields are not added with a null value.
  3. Nothing was sent, so the result has no keys.
  4. 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;
}

How It Works

  1. FILTER_SANITIZE_EMAIL removes characters that cannot appear in an email address.
  2. The age field uses an array with filter and options keys to set a range.
  3. The script first checks that an array came back and that age was sent.
  4. 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.

Next: filter_list()

filter_list() returns an array with the names of all filters that your PHP installation su.

filter_list() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful