In PHP, filter_var_array is a very practical function for filtering and validating data in an array. Many developers will encounter a question: Can filter_var_array handle multi-dimensional arrays? This article will answer this question in detail and introduce its usage and precautions.
The filter_var_array function can apply specified filters to each element of an array, and is often used to process user input data, such as $_GET, $_POST, or $_COOKIE arrays. The basic usage is as follows:
<?php
$data = [
'email' => 'user@example.com',
'age' => '25',
];
$filters = [
'email' => FILTER_VALIDATE_EMAIL,
'age' => [
'filter' => FILTER_VALIDATE_INT,
'options' => ['min_range' => 18, 'max_range' => 65]
],
];
$result = filter_var_array($data, $filters);
var_dump($result);
?>
The above code will verify that the mailbox is valid and check whether the age is between 18 and 65.
By default, filter_var_array only processes one-dimensional arrays, that is, it will only iterate over the first layer of the array and apply filtering rules for each key-value pair. For multidimensional arrays, it does not process inner layer arrays recursively.
For example:
<?php
$data = [
'user' => [
'email' => 'user@example.com',
'age' => '25',
],
'status' => 'active'
];
$filters = [
'user' => FILTER_DEFAULT, // Here's right"user"Corresponding to an array,By default, the internal elements are not filtered
'status' => FILTER_SANITIZE_STRING,
];
$result = filter_var_array($data, $filters);
var_dump($result);
?>
In the result, the array corresponding to user will be returned as is and will not be filtered.
If you need to filter multidimensional arrays, filter_var_array itself cannot directly support recursive operations. A common practice is to write a recursive function, iterate through a multi-dimensional array, call filter_var_array for each layer, or use filter_var .
Sample code:
<?php
function recursive_filter_var_array(array $data, array $filters) {
$result = [];
foreach ($filters as $key => $filter) {
if (is_array($filter) && isset($data[$key]) && is_array($data[$key])) {
// Recursively process multidimensional arrays
$result[$key] = recursive_filter_var_array($data[$key], $filter);
} elseif (isset($data[$key])) {
// Filter individual elements
if (is_array($filter)) {
$result[$key] = filter_var($data[$key], $filter['filter'], $filter['options'] ?? []);
} else {
$result[$key] = filter_var($data[$key], $filter);
}
}
}
return $result;
}
// Sample data and filtering rules
$data = [
'user' => [
'email' => 'user@example.com',
'age' => '30',
],
'status' => 'active<script>',
];
$filters = [
'user' => [
'email' => FILTER_VALIDATE_EMAIL,
'age' => [
'filter' => FILTER_VALIDATE_INT,
'options' => ['min_range' => 18, 'max_range' => 65],
],
],
'status' => FILTER_SANITIZE_STRING,
];
$result = recursive_filter_var_array($data, $filters);
var_dump($result);
?>
Through this recursive function, you can flexibly process multi-dimensional arrays and filter data hierarchically.
The filtering rules and array structure must correspond to <br> The structure of the filtering rule array must match the structure of the filtered array, otherwise it will cause filtering failure or data loss.
filter_var_array does not support recursion <br> By default, only one-dimensional arrays are processed, and you need to write recursive functions yourself to implement multi-dimensional filtering.
Reasonable filter selection <br> Select the appropriate filter according to the data type, such as FILTER_VALIDATE_EMAIL for mailboxes, FILTER_VALIDATE_INT for integers, and FILTER_SANITIZE_STRING for strings, etc.
Option Configuration <br> Options can be set for filters, such as range limits, default values, etc., to enhance the flexibility and security of filtering.
filter_var_array is a powerful array filtering tool in PHP, but it only supports one-dimensional arrays. If you want to deal with multi-dimensional arrays, it is recommended to implement the recursive filtering function yourself. By rationally designing filtering rules and structures, data security and effectiveness can be effectively guaranteed.
For more PHP related tips and examples, you can visit https://gitbox.net/php/filter_var_array for details.