Filter: Add examples for `filter_var_array()` and `filter_input_array()`
These functions are very confusing and needs a bunch of examples.
filter_var_array() function is used to filter multiple variables at once, by validating and sanitizing user input, particularly when you need to handle an array of data.
Sintax
filter_var_array(array $data, array $filters): array|false
Example
$data = [ 'email' => '[email protected]', 'url' => 'http://example.com', 'age' => '25' ];
$filters = [ 'email' => FILTER_VALIDATE_EMAIL, 'url' => FILTER_VALIDATE_URL, 'age' => FILTER_VALIDATE_INT ];
$filtered_data = filter_var_array($data, $filters);
print_r($filtered_data);
Conclusion
In the above example, filter_var_array() validates the email, URL and age fields according to the specified filters. Returns an array of filtered values or false if validation fails.
filter_input_array() function is similar but it is typically used for filtering input data coming from external sources like $_GET $_POST or $_REQUEST
Sintax
filter_input_array(int $type, array $filters, bool $add_empty = true): array|false
Example
// Example of using filter_input_array() to filter GET input $filters = [ 'email' => FILTER_VALIDATE_EMAIL, 'age' => FILTER_VALIDATE_INT ];
// Filter input from the GET request $filtered_input = filter_input_array(INPUT_GET, $filters);
print_r($filtered_input);
Conclusion
Above code filters the email and age values from the $_GET array. It validates the email as an email address and the age as an integer.