PHP Filter_var_array Function

What is filter_var_array() in PHP?

Explanation

filter_var_array() is a function which validates and filters multiple values stored in the variables.

Syntax:


filter_var_array(array, args)

In the above syntax, 'array' is the variables to be filtered and validated. 'args' is the filters used to validate.

This function validates for multiple values without calling filter_var() many times.

Example :


<?php
$arr = array
(
"name" => "hscripts.com",
"age" => "10",
"email" => "support@hscripts.com",
);
$filters = array
(
"name" => array
(
"filter"=>FILTER_CALLBACK,
"flags"=>FILTER_FORCE_ARRAY,
"options"=>"ucwords"
),
"age" => array
(
"filter"=>FILTER_VALIDATE_INT,
"options"=>array
(
"min_range"=>1,
"max_range"=>120
)
),
"email"=> FILTER_VALIDATE_EMAIL,
);
print_r(filter_var_array($arr, $filters));
?>

Result :

Array ( [name] => Hscripts.com [age] => 10 [email] => support@hscripts.com )

In the above example, the values stored in the array $arr is filtered and validated using the $filters, which is an array of filters.

PHP Topics


Ask Questions

Ask Question