Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / PHP

filter_var_array()

Filtra un array fornito con regole per nome di campo.

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

Sintassi

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

Filtra un array fornito con regole per nome di campo. Con i flag normali un campo non valido vale false. Un campo richiesto assente diventa null con add_empty=true e viene omesso con false. Le chiavi fuori dalla mappa delle regole sono escluse. FILTER_REQUIRE_ARRAY richiede un array e filtra ogni elemento. FILTER_NULL_ON_FAILURE assegna null agli elementi non validi.

Firma

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

Output dell’esempio

[{"qty":3,"level":false,"memo":null},{"qty":3,"level":false}]
{"scores":[3,null]}

Esempi

<?php
$data=["qty"=>"3","level"=>"x","extra"=>7]; $rules=["qty"=>FILTER_VALIDATE_INT,"level"=>FILTER_VALIDATE_INT,"memo"=>FILTER_DEFAULT];
echo json_encode([filter_var_array($data,$rules),filter_var_array($data,$rules,false)]);
$rule=["scores"=>["filter"=>FILTER_VALIDATE_INT,"flags"=>FILTER_REQUIRE_ARRAY | FILTER_NULL_ON_FAILURE]];
echo "\n",json_encode(filter_var_array(["scores"=>["3","x"]],$rule));

Risultato

[{"qty":3,"level":false,"memo":null},{"qty":3,"level":false}]
{"scores":[3,null]}

Fonti