CKTIS / PHP
filter_var_array()
Filtra un array suministrado según reglas por nombre de campo.
Versión: PHP 8.4; repository runtime, production runtime not attested
Sintaxis
filter_var_array(array $data, array|int $options = FILTER_DEFAULT, bool $add_empty = true): array|false|nullFiltra un array suministrado según reglas por nombre de campo. Con las opciones normales un campo inválido vale false. Un campo pedido ausente recibe null con add_empty=true y se omite con false. Las claves fuera del mapa de reglas se excluyen. FILTER_REQUIRE_ARRAY exige un array y filtra cada elemento. FILTER_NULL_ON_FAILURE da null para los elementos inválidos.
Firma
filter_var_array(array $data, array|int $options = FILTER_DEFAULT, bool $add_empty = true): array|false|null
Salida del ejemplo
[{"qty":3,"level":false,"memo":null},{"qty":3,"level":false}]
{"scores":[3,null]}
Ejemplos
<?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));
Resultado
[{"qty":3,"level":false,"memo":null},{"qty":3,"level":false}]
{"scores":[3,null]}