Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

filter_var_array()

Example

Use the filter_var_array() function to filter multiple variables at once.

<?php
$data = array(
  ‘fullname’ => ‘Peter Griffin’,
  ‘age’ => ’41’,
  ’email’ => [email protected],
);

$mydata = filter_var_array($data);
var_dump($mydata);

?>

The result of the code should be:

array(3) {
  [“fullname”]=>
  string(13) “Peter Griffin”
  [“age”]=>
  string(2) “41”
  [“email”]=>
  string(17) “[email protected]
}

Definition and Usage

The filter_var_array() function handles multiple variables and can optionally filter them. This function is useful for filtering several values at once, avoiding multiple calls to filter_var().

 

Tip: Consult the PHP Filter Reference for available filters that can be used with this function.

Syntax

filter_var_array(data_array, args, add_empty)

Parameter Values

 

Parameter

Description

data_array

Mandatory. Specifies an array with string keys that contains the data to be filtered.

args

Optional. Specifies an array of filter arguments. The array keys should be variable names, and the values can be either a filter ID or an array detailing the filter, flags, and options. Alternatively, a single filter ID can be provided, in which case all values in the input array will be filtered using that filter. The filter ID can be a name (e.g., FILTER_VALIDATE_EMAIL) or a number (e.g., 274).

add_empty

Optional. A boolean value. Setting it to TRUE will include missing keys as NULL in the return value. The default is TRUE.

Technical Details

Return Value:

An array of the requested variable values if successful, or FALSE if the operation fails.

PHP Version:

5.2+

PHP Changelog:

In PHP 5.4, the add_empty parameter was introduced.