Open In App

PHP | filter_input_array() Function

The filter_input_array() function is an inbuilt function in PHP which is used to get external variables (e.g. from form input) and filters them if it is specified. This function is similar to filter_input() function but the only difference is filter_input() filters a single value but in filter_input_array() filters the whole array according to options provided. It is useful for retrieving/filtering many values instead of calling filter_input() many times. This is new in PHP only works in PHP 5.2 or latest version of PHP. Syntax:

mixed filter_input_array( $type_of_data, $definition, $add_empty_parameter )

Parameters: This function accepts three parameters as mentioned above and described below:



Return Value: It returns an array containing the values of the variables on success and on failure, it returns False. If the input array designated by type is not populated, the function returns NULL if the FILTER_NULL_ON_FAILURE flag is not given, otherwise, it returns False. For other failures, False is returned. Below programs illustrate the filter_input_array() function in PHP: program 1: 




<?php
 
$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_input_array(INPUT_GET, $filters));
 
?>

Output: Note: This example may not give the expected result on online IDE as it doesn’t support the passing of parameters in GET or POST method. So, try to run it in some PHP hosting server or localhost and pass the values of the parameter either by GET or POST method. Program 2: Data came from POST method:



$_POST = array(
    'product_id' => '234<A>',
    'component'  => array('10'),
    'version'    => '<2.8.9',
    'array2'  => array('45', '1'),
    'scalar_data' => '2',
);




<?php
// PHP program to uses filter_input_array() Function
 
error_reporting(E_ALL | E_STRICT);
 
$args = array(
    'id' => FILTER_SANITIZE_ENCODED,
    'array1' => array(
        'filter' => FILTER_VALIDATE_INT,
        'flags' => FILTER_REQUIRE_ARRAY,
        'options' => array(
            'min_range' => 1,
            'max_range' => 10
        )
    ),
    'version' => FILTER_SANITIZE_ENCODED,
    'noparameter' => FILTER_VALIDATE_INT,
    'scalar_data' => array(
        'filter' => FILTER_VALIDATE_INT,
        'flags'  => FILTER_REQUIRE_SCALAR,
    ),
    'array2' => array(
        'filter' => FILTER_VALIDATE_INT,
        'flags'  => FILTER_REQUIRE_ARRAY,
    )
);
   
$allinputs = filter_input_array(INPUT_GET, $args);
   
var_dump($allinputs);
echo "\n";
 
?>

Output: Note: This example may not give the expected result on online IDE as it doesn’t support the passing of parameters in GET or POST method. So, try to run it in some PHP hosting server or localhost and pass the values of the parameter either by GET or POST method. References: http://php.net/manual/en/function.filter-input-array.php


Article Tags :