PHP filter_input_array()ฟังก์ชั่น
ตัวอย่าง
ใช้ฟังก์ชัน filter_input_array() เพื่อกรองตัวแปร POST สามตัว ตัวแปร POST ที่ได้รับคือชื่อ อายุ และอีเมล:
<?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_POST, $filters));
?>
ผลลัพธ์ของโค้ดด้านบนจะเป็น:
ความหมายและการใช้งาน
ฟังก์ชัน filter_input_array() รับตัวแปรภายนอก (เช่น จากอินพุตแบบฟอร์ม) และกรองตัวแปรเหล่านี้ได้
ฟังก์ชันนี้มีประโยชน์สำหรับการดึง/กรองค่าจำนวนมากแทนที่จะเรียก filter_input() หลายครั้ง
ไวยากรณ์
filter_input_array(type, definition, add_empty)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
type | Required. The input type to check for. Can be one of the following:
|
definition | Optional. Specifies an array of filter arguments. A valid array key is a variable name, and a valid value is a filter name or ID, or an array specifying the filter, flags and options. This parameter can also be a single filter name/ID; then all values in the input array are filtered by the specified filter |
add_empty | Optional. A Boolean value. TRUE adds missing keys as NULL to the return value. Default value is TRUE |
รายละเอียดทางเทคนิค
คืนมูลค่า: | อาร์เรย์ที่มีค่าของตัวแปรในความสำเร็จ FALSE เมื่อล้มเหลว |
---|---|
รุ่น PHP: | 5.2+ |
บันทึกการเปลี่ยนแปลง PHP: | PHP 5.4 - เพิ่มพารามิเตอร์add_empty แล้ว |
❮ อ้างอิงตัวกรอง PHP ที่สมบูรณ์