filter_input_array
Description
The filter_input_array of Filter for PHP gets external variables and optionally filters them.
Syntax
filter_input_array(
int $type,
array|int $options = FILTER_DEFAULT,
bool $add_empty = true
): array|false|nullParameters
type
One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV.
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
On success, an array containing the values of the requested variables.
On failure, false is returned. Except if the failure is that the input array designated by type is not populated where null is returned if the FILTER_NULL_ON_FAILURE flag is used.
Missing entries from the input array will be populated into the returned array if add_empty is true. In which case, missing entries will be set to null, unless the FILTER_NULL_ON_FAILURE flag is used, in which case it will be false.
An entry of the returned array will be false if the filter fails, unless the FILTER_NULL_ON_FAILURE flag is used, in which case it will be null.
Examples
1 · type
<? // $_GET["myvariable"] = 1; $type = INPUT_GET; $return = filter_input_array($type); var_export($return);
NULL
2 · options
<?
$type = INPUT_GET;
$options =
[
"myvariable" => FILTER_VALIDATE_BOOLEAN
];
$return = filter_input_array($type, $options);
var_export($return);
NULL
3 · add_empty
<?
$type = INPUT_GET;
$options =
[
"myvariable" => FILTER_VALIDATE_BOOLEAN
];
$add_empty = false;
$return = filter_input_array($type, $options, $add_empty);
var_export($return);
NULL
4 · INPUT_POST
<?
error_reporting(E_ALL | E_STRICT);
/* data from POST
$_POST =
[
"product_id" => "libgd<script>",
"component" => ["10"],
"version" => "2.0.33",
"testarray" => ["2", "23", "10", "12"],
"testscalar" => "2",
];
*/
$type = INPUT_POST;
$options =
[
"product_id" => FILTER_SANITIZE_ENCODED,
"component" =>
[
"filter" => FILTER_VALIDATE_INT,
"flags" => FILTER_REQUIRE_ARRAY,
"options" =>
[
"min_range" => 1,
"max_range" => 10,
],
],
"version" => FILTER_SANITIZE_ENCODED,
"doesnotexist" => FILTER_VALIDATE_INT,
"testscalar" =>
[
"filter" => FILTER_VALIDATE_INT,
"flags" => FILTER_REQUIRE_SCALAR,
],
"testarray" =>
[
"filter" => FILTER_VALIDATE_INT,
"flags" => FILTER_REQUIRE_ARRAY,
],
];
$return = filter_input_array($type, $options);
var_dump($return);
NULL