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
$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
- The data array holds text values just like form input.
- The definition array gives email an email filter and age an integer filter.
- filter_var_array() applies the matching filter to each key.
- json_encode() shows the result: the age is now the number 25, not the string '25'.

