filter_input_array

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

filter_input_array获取一系列外部变量,并且可以通过过滤器处理它们

说明

filter_input_array(int $type, array|int $options = FILTER_DEFAULT, bool $add_empty = true): array|false|null

这个函数当需要获取很多变量却不想重复调用filter_input()时很有用。

参数

type

INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV之一。

options

一个定义参数的数组。一个有效的键必须是一个包含变量名的string,一个有效的值要么是一个filter type,或者是一个array 指明了过滤器、标示和选项。如果值是一个数组,那么它的有效的键可以是 filter, 用于指明 filter typeflags 用于指明任何想要用于过滤器的标示,或者 options 用于指明任何想要用于过滤器的选项。 参考下面的例子来更好的理解这段说明。

这个参数也可以是一个filter constant的整数。那么数组中的所有变量都会被这个过滤器所过滤。

add_empty

在返回值中添加 null 作为不存在的键。

返回值

成功时包含请求变量值的数组。如果通过 type 指定的输入数组没有值可以填充,当没有指定 FILTER_NULL_ON_FAILURE flag 时函数返回 null,否则为 false。其它失败返回 false

如果过滤失败,数组值将为 false,如果变量未设置,返回 null。如果使用 FILTER_NULL_ON_FAILURE flag,变量未设置将返回 false,过滤失败将返回 null。如果 add_empty 参数为 false,不会为复位的变量添加数组元素。

范例

示例 #1 filter_input_array() 示例

<?php

/* 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',
);
*/

$args = 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,
)

);

$myinputs = filter_input_array(INPUT_POST, $args);

var_dump($myinputs);
echo
"\n";
?>

以上例程会输出:

array(6) {
  ["product_id"]=>
  string(17) "libgd%3Cscript%3E"
  ["component"]=>
  array(1) {
    [0]=>
    int(10)
  }
  ["version"]=>
  string(6) "2.0.33"
  ["doesnotexist"]=>
  NULL
  ["testscalar"]=>
  int(2)
  ["testarray"]=>
  array(4) {
    [0]=>
    int(2)
    [1]=>
    int(23)
    [2]=>
    int(10)
    [3]=>
    int(12)
  }
}

注释

注意:

INPUT_SERVER 数组中并没有 REQUEST_TIME ,因为它是被稍后插入到 $_SERVER 中的。

参见

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top