Filter Funciones
PHP Manual

filter_input_array

(PHP 5 >= 5.2.0)

filter_input_arrayObtiene variables externas y opcionalmente las filtra

Descripción

mixed filter_input_array ( int $type [, mixed $definition ] )

Esta función es útil para recuperar muchos valores sin necesidad de hacer repetidas llamadas a filter_input().

Parámetros

type

Una de las siguientes: INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER o INPUT_ENV.

definition

Un array de clave-valor que define los argumentos. Una clave válida es un string que contiene un nombre de variable y un valor válido es tanto un tipo de filtro o un array especificando opcionalmente el filtro, flags y opciones. Si el valor es un array, las claves válidas son filter que especifica el tipo de filtro, flags que especifica cualquier parámetro que se aplica al filtro y options que especifica cualquier opción que se aplica al filtro. Más adelante hay ejemplos que ayudan a un mejor entendimiento.

Este parámetro puede ser también un entero que indique una constante de filtro. Entonces todos los valores en el array de entrada son filtrados por ese filtro.

Valores devueltos

En caso de éxito, un array con los valores de las variables que se han pedido o FALSE en caso de fallo. Un valor del array puede ser FALSE si el filtro falla o NULL si la variable no está creada. O, si se usa el flag FILTER_NULL_ON_FAILURE y la variable no está definida retorna FALSE y NULL si el filtro falla.

Ejemplos

Example #1 Un ejemplo con filter_input_array()

<?php
error_reporting
(E_ALL E_STRICT);
/* Los datos vienen realmente por POST
$_POST = array(
    'id_producto'    => 'libgd<script>',
    'componente'     => '10',
    'version'      => '2.0.33',
    'test_escalar'    => array('2', '23', '10', '12'),
    'test_array'     => '2',
);
*/

$args = array(
    
'id_producto'   => FILTER_SANITIZE_ENCODED,
    
'componente'    => array('filter'    => FILTER_VALIDATE_INT,
                            
'flags'     => FILTER_REQUIRE_ARRAY
                            
'options'   => array('min_range' => 1'max_range' => 10)
                           ),
    
'version'     => FILTER_SANITIZE_ENCODED,
    
'no_existe' => FILTER_VALIDATE_INT,
    
'test_escalar'   => array(
                            
'filter' => FILTER_VALIDATE_INT,
                            
'flags'  => FILTER_REQUIRE_SCALAR,
                           ),
    
'test_array'    => array(
                            
'filter' => FILTER_VALIDATE_INT,
                            
'flags'  => FILTER_REQUIRE_ARRAY,
                           )

);

$mis_entradas filter_input_array(INPUT_POST$args);

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

El resultado del ejemplo sería:

array(6) {
  ["id_producto"]=>
  array(1) {
    [0]=>
    string(17) "libgd%3Cscript%3E"
  }
  ["componente"]=>
  array(1) {
    [0]=>
    int(10)
  }
  ["version"]=>
  array(1) {
    [0]=>
    string(6) "2.0.33"
  }
  ["no_existe"]=>
  NULL
  ["test_escalar"]=>
  bool(false)
  ["test_array"]=>
  array(1) {
    [0]=>
    int(2)
  }
}

Ver también


Filter Funciones
PHP Manual