filter_var_array
Description
The filter_var_array of Filter for PHP gets multiple variables and optionally filters them.
Syntax
filter_var_array( array $array, array|int $options = FILTER_DEFAULT, bool $add_empty = true ): array|false|null
Parameters
array
An array with string keys containing the data to filter.
options
An array defining the arguments. A valid key is a string containing a variable name and a valid value is either a filter type, or an array optionally specifying the filter, flags and options. If the value is an array, valid keys are filter which specifies the filter type, flags which specifies any flags that apply to the filter, and options which specifies any options that apply to the filter. See the example below for a better understanding.
This parameter can be also an integer holding a filter constant. Then all values in the input array are filtered by this filter.
add_empty
Add missing keys as null to the return value.
Return
Returns an array containing the values of the requested variables on success, or false on failure. An array value will be false if the filter fails, or null if the variable is not set.
Examples
1 · array
<? $array = array( "myvariable1" => 0, "myvariable2" => array(0, 1) ); $return = filter_var_array($array); print_r($return); ?>
Array ( [myvariable1] => 0 [myvariable2] => Array ( [0] => 0 [1] => 1 ) )
2 · options
<? $array = array( "myvariable1" => 0, "myvariable2" => array(0, 1) ); $options = array( "myvariable1" => array( "filter" => FILTER_VALIDATE_INT, "flags" => FILTER_FORCE_ARRAY, "options" => array( "min_range" => 0, "max_range" => 10 ) ), "myvariable2" => array( "filter" => FILTER_VALIDATE_INT, "flags" => FILTER_FORCE_ARRAY, "options" => array( "min_range" => 1, "max_range" => 10 ) ), "myvariable3" => FILTER_DEFAULT ); $return = filter_var_array($array, $options); print_r($return); ?>
Array ( [myvariable1] => Array ( [0] => 0 ) [myvariable2] => Array ( [0] => [1] => 1 ) [myvariable3] => )
3 · add_empty
<? $array = array( "myvariable1" => 0, "myvariable2" => array(0, 1) ); $options = array( "myvariable1" => array( "filter" => FILTER_VALIDATE_INT, "flags" => FILTER_FORCE_ARRAY, "options" => array( "min_range" => 0, "max_range" => 10 ) ), "myvariable2" => array( "filter" => FILTER_VALIDATE_INT, "flags" => FILTER_FORCE_ARRAY, "options" => array( "min_range" => 1, "max_range" => 10 ) ), "myvariable3" => FILTER_DEFAULT ); $add_empty = false; $return = filter_var_array($array, $options, $add_empty); print_r($return); ?>
Array ( [myvariable1] => Array ( [0] => 0 ) [myvariable2] => Array ( [0] => [1] => 1 ) )
4
<? error_reporting(E_ALL | E_STRICT); $array = array( "product_id" => "libgd<script>", "component" => "10", "versions" => "2.0.33", "testscalar" => array("2", "23", "10", "12"), "testarray" => "2", ); $options = array( "product_id" => FILTER_SANITIZE_ENCODED, "component" => array( "filter" => FILTER_VALIDATE_INT, "flags" => FILTER_FORCE_ARRAY, "options" => array( "min_range" => 1, "max_range" => 10 ) ), "versions" => FILTER_SANITIZE_ENCODED, "doesnotexist" => FILTER_VALIDATE_INT, "testscalar" => array( "filter" => FILTER_VALIDATE_INT, "flags" => FILTER_REQUIRE_SCALAR, ), "testarray" => array( "filter" => FILTER_VALIDATE_INT, "flags" => FILTER_FORCE_ARRAY, ) ); $return = filter_var_array($array, $options); var_export($return); ?>
array ( 'product_id' => 'libgd%3Cscript%3E', 'component' => array ( 0 => 10, ), 'versions' => '2.0.33', 'doesnotexist' => NULL, 'testscalar' => false, 'testarray' => array ( 0 => 2, ), )