Content · Programming · PHP

PHP filter_var_array() Function: Usage and Examples

Understand filter_var_array() in PHP with syntax, working code examples, expected output, practical use cases, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When writing PHP applications, you may need to apply validation and sanitization rules to several values in a PHP array. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.

The filter_var_array() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.

What Is filter_var_array() in PHP?

Apply validation and sanitization rules to several values in a PHP array.

Its syntax is:

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

Parameters used by this operation:

  • $array — The collection being examined or updated.
  • $options — The value supplied for this argument affects the operation.
  • $add_empty — The value supplied for this argument affects the operation.

Invalid values commonly yield false; unspecified fields are omitted and rules for absent fields may yield null. Sanitization is not authorization or output escaping.

Example 1: Basic Usage

Start with a small, self-contained input so you can inspect exactly how the operation behaves.

<?php

$input = ["age" => "31", "email" => "[email protected]"];
$rules = ["age" => FILTER_VALIDATE_INT, "email" => FILTER_VALIDATE_EMAIL];
print_r(filter_var_array($input, $rules));

Output:

Array
(
    [age] => 31
    [email] => [email protected]
)

The output shows the result of the first call. Check the printed keys and values as well as their order.

Example 2: Another Important Case

This variation highlights another input shape, a comparison option, or a change in the collection.

<?php

$input = ["page" => "5", "name" => "Maya"];
$rules = ["page" => FILTER_VALIDATE_INT];
print_r(filter_var_array($input, $rules));

Output:

Array
(
    [page] => 5
)

Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.

Practical Example: Check values already loaded from JSON or a form before saving them

Check values already loaded from JSON or a form before saving them. Save the code in a PHP file and adapt the sample data to your project.

<?php

$formValues = ["email" => "[email protected]", "page" => "3"];
$validationRules = ["email" => FILTER_VALIDATE_EMAIL, "page" => FILTER_VALIDATE_INT];
$validated = filter_var_array($formValues, $validationRules);
if ($validated !== false && $validated["email"] !== false && $validated["page"] !== false) { echo "Valid page: " . $validated["page"]; }

Output:

Valid page: 3

The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.

Important Behavior and Edge Cases

Invalid values commonly yield false; unspecified fields are omitted and rules for absent fields may yield null. Sanitization is not authorization or output escaping.

  • Test the operation with empty or missing input and decide how your application should respond.
  • Read the return value independently from any changes to variables passed by reference.
  • Validate untrusted input before using the result for permissions, database changes, or user-visible output.

Comparison With Related Functions

Function or ApproachWhen to Use It
filter_var_array()Apply validation and sanitization rules to several values in a PHP array
Related functionsfilter_input_array() reads external request input; filter_var() validates one value.

Common Beginner Mistakes

MistakeCorrect Approach
Treating a missing value as validCheck absent fields separately from fields that fail validation.
Assuming validation authorizes a changeValidation checks data format; permissions require separate enforcement.
Checking only the top-level resultInvalid values commonly yield false; unspecified fields are omitted and rules for absent fields may yield null. Sanitization is not authorization or output escaping.

Key Takeaways

  • filter_var_array() helps you apply validation and sanitization rules to several values in a PHP array.
  • Invalid values commonly yield false; unspecified fields are omitted and rules for absent fields may yield null. Sanitization is not authorization or output escaping.
  • filter_input_array() reads external request input; filter_var() validates one value.
  • Use the practical example as a starting point, but validate real application inputs.

filter_var_array() is useful when you need to apply validation and sanitization rules to several values in a PHP array. Use its documented rules and the examples above to choose the right behavior for your data.

Share this resourceSend this page to someone who may find it useful.