Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / PHP

filter_input_array()

Filtra più campi originali della SAPI con un filtro comune o regole per campo.

Versione: PHP 8.4; repository runtime, production runtime not attested

Sintassi

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

Filtra più campi originali della SAPI con un filtro comune o regole per campo. Se la sorgente esiste, add_empty aggiunge con null le chiavi richieste assenti. In CLI senza sorgente GET l’intero risultato è null anche dopo aver modificato $_GET. Con una sorgente presente e FILTER_NULL_ON_FAILURE una chiave assente rimane null con add_empty=true. Il singolo filter_input per quella chiave dà false.

Firma

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

Output dell’esempio

[{"qty":6,"memo":null},false]

Esempi

<?php
if (PHP_SAPI==="cgi-fcgi") {
$rules=["qty"=>FILTER_VALIDATE_INT,"memo"=>["filter"=>FILTER_VALIDATE_INT,"flags"=>FILTER_NULL_ON_FAILURE]];
    echo json_encode([filter_input_array(INPUT_GET,$rules,true),filter_input(INPUT_GET,"memo",FILTER_VALIDATE_INT,FILTER_NULL_ON_FAILURE)]);
    exit;
}
$env=["REDIRECT_STATUS"=>"1","SCRIPT_FILENAME"=>__FILE__,"REQUEST_METHOD"=>"GET","QUERY_STRING"=>"qty=6"];
$proc=proc_open([PHP_BINDIR."/php-cgi"],[1=>["pipe","w"]],$pipes,null,$env);
$response=stream_get_contents($pipes[1]); fclose($pipes[1]); proc_close($proc);
[$headers,$body]=preg_split("/\\r?\\n\\r?\\n/",$response,2);
echo $body;

Risultato

[{"qty":6,"memo":null},false]

Fonti