PHP filter_var_array()Functions


PHP Filter 参考手册Complete PHP Filter Reference Manual

Definition and Usage

The filter_var_array() function gets multiple variables and filters them.

This function is useful for filtering multiple values, without needing to call filter_var() repeatedly.

If successful, it returns the values of the requested variables as an array. If it fails, it returns FALSE.

Syntax

filter_var_array(array, args)

Parameters Description
array Required. Specifies an array with string keys, containing the data to be filtered.
args Optional. Specifies an array of filter parameters. Valid array key names are variable names, and valid values are filter IDs, or an array specifying the filter, flags, and options.

This parameter can also be a single filter ID. If so, all values in the input array are filtered by the specified filter.

The filter ID can be an ID name (such as FILTER_VALIDATE_EMAIL) or an ID number (such as 274).


Tips and Notes

Tip:See alsoComplete PHP Filter Reference Manualto view the filters that can be used with this function.


Examples

<?php
$arr = array
(
"name" => "peter griffin",
"age" => "41",
"email" => "[email protected]",
);

$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));
?>

The output of the code is as follows:

Array
(
[name] => Peter Griffin
[age] => 41
[email] => [email protected]
)


PHP Filter 参考手册Complete PHP Filter Reference Manual Other Extensions