PHP filter_var_array() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filter

Definition

In PHP, filter_var_array() filters many values from an array at once, and each key can have its own filter.

It is like calling filter_var() on every item of an array in one step. This is useful for checking a whole set of form fields that you already have in a variable.

Syntax

Call filter_var_array() with the values you need. Start with this line.

PHP
$clean = filter_var_array($data, $definition);
CallWhat it does
filter_var_array($data, ['email' => FILTER_VALIDATE_EMAIL, 'age' => FILTER_VALIDATE_INT])Filters each key with its own rule and returns the new array.
filter_var_array(['a' => '1', 'b' => 'x'], FILTER_VALIDATE_INT)Applies one filter to every item. a becomes 1 and b becomes false.
filter_var_array($data, $definition, false)Leaves out keys from the definition that are missing in the data.

The second argument can be a single filter ID for every item, or an array that maps each key to a filter ID or to an array with 'filter', 'flags', and 'options'. Keys in the data that are not in the definition are not included in the result.

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

Parameters

ParameterDescription
arrayRequired. The array of values to filter.
optionsOptional. A filter ID used for every item, or an array that defines the filter for each key. The default is FILTER_DEFAULT.
add_emptyOptional. If true, keys from the definition that are missing in the data are added with a null value. If false, they are left out. The default is true.

Return Value

Return ValueDescription
arrayAn array of filtered values. A failed filter gives false for that key, and a missing key gives null when add_empty is true.
falseReturned in rare failure cases, for example when a flag changes how failure is reported.
nullReturned when a failure flag such as FILTER_NULL_ON_FAILURE changes the result.

PHP Examples

Example 1 — Validate a Small Form Array

Filter an email and an age together, each with its own filter. The age string becomes a real integer.

PHP
<?php
$data = ['email' => '[email protected]', 'age' => '25'];

$clean = filter_var_array($data, [
    'email' => FILTER_VALIDATE_EMAIL,
    'age'   => FILTER_VALIDATE_INT,
]);

echo json_encode($clean), PHP_EOL;

How It Works

  1. The data array holds text values just like form input.
  2. The definition array gives email an email filter and age an integer filter.
  3. filter_var_array() applies the matching filter to each key.
  4. json_encode() shows the result: the age is now the number 25, not the string '25'.

Example 2 — Invalid and Missing Fields

An invalid value becomes false, and a missing key becomes null unless you turn off add_empty.

PHP
<?php
$data = ['email' => 'not-an-email'];

$definition = [
    'email' => FILTER_VALIDATE_EMAIL,
    'age'   => FILTER_VALIDATE_INT,
];

echo json_encode(filter_var_array($data, $definition)), PHP_EOL;
echo json_encode(filter_var_array($data, $definition, false)), PHP_EOL;

How It Works

  1. The email value fails FILTER_VALIDATE_EMAIL, so its result is false.
  2. There is no age key in the data, so the first call adds it as null (add_empty is true).
  3. The second call passes false for add_empty, so the age key is left out.
  4. Always check each key for false or null before using the data.

Example 3 — Sanitize and Limit with Options

Use an array rule to set a range for age, and a sanitize filter for the email field.

PHP
<?php
$data = [
    'email' => 'ana(test)@example.com',
    'age'   => '17',
    'role'  => 'admin',
];

$clean = filter_var_array($data, [
    'email' => FILTER_SANITIZE_EMAIL,
    'age'   => [
        'filter'  => FILTER_VALIDATE_INT,
        'options' => ['min_range' => 18, 'max_range' => 65],
    ],
]);

echo json_encode($clean), PHP_EOL;

How It Works

  1. FILTER_SANITIZE_EMAIL removes the parentheses from the email.
  2. The age rule uses an array with filter and options keys to set a range of 18 to 65.
  3. 17 is below the minimum, so age is false.
  4. The role key is not in the definition, so it is dropped from the result.

Frequently Asked Questions

It filters several values in an array in one call. You can use the same filter for all of them or give each key its own filter.
filter_var() handles one value. filter_var_array() handles a whole array and returns a new array with the filtered values.
Only keys listed in your definition are returned. If a key is not in the definition it is dropped. If the key is in the definition but missing from the data, it appears as null only when add_empty is true.
When it is true, keys in the definition that are not found in the data are added to the result as null. When it is false, those keys are left out.
You can call filter_var_array($_POST, $definition), but filter_input_array(INPUT_POST, $definition) is usually better because it reads the original request and ignores later changes to $_POST.

Back to Filter Functions

Browse all PHP filter function tutorials.

Filter functions →

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