HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

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|null

Parameters

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

Returns an array containing the values of the requested variables on success. If the input array designated by type is not populated, the function returns null if the FILTER_NULL_ON_FAILURE flag is not given, or false otherwise. For other failures, false is returned.

An array value will be false if the filter fails, or null if the variable is not set. Or if the flag FILTER_NULL_ON_FAILURE is used, it returns false if the variable is not set and null if the filter fails. If the add_empty parameter is false, no array element will be added for unset variables.

Examples

1 · type

<?

$type = INPUT_GET;

$return = filter_input_array($type);

var_export($return);
NULL

2 · options

<?

$type = INPUT_GET;
$options = array("myvariable" => FILTER_VALIDATE_BOOLEAN);

$return = filter_input_array($type, $options);

var_export($return);
NULL

3 · add_empty

<?

$type = INPUT_GET;
$options = array("myvariable" => FILTER_VALIDATE_BOOLEAN);
$add_empty = false;

$return = filter_input_array($type, $options, $add_empty);

var_export($return);
NULL

4

<?

error_reporting(E_ALL | E_STRICT);

/* data actually came from POST
$_POST = array(
    "product_id" => "libgd<script>",
    "component" => array("10"),
    "version" => "2.0.33",
    "testarray" => array("2", "23", "10", "12"),
    "testscalar" => "2",
);
*/

$type = INPUT_POST;
$options = array(
    "product_id" => FILTER_SANITIZE_ENCODED,
    "component" => array(
        "filter" => FILTER_VALIDATE_INT,
        "flags" => FILTER_REQUIRE_ARRAY, 
        "options" => array(
            "min_range" => 1,
            "max_range" => 10,
        ),
    ),
    "version" => 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_REQUIRE_ARRAY,
    ),
);

$return = filter_input_array($type, $options);

var_dump($return);
NULL