Home >Backend Development >PHP Tutorial >Use of php array_filter function (detailed explanation)

Use of php array_filter function (detailed explanation)

烟雨青岚
烟雨青岚forward
2020-06-08 16:42:096843browse

Use of php array_filter function (detailed explanation)

php array_filter function usage (detailed explanation)

1, array_filter

(PHP 4 >= 4.0.6, PHP 5, PHP 7)

array_filter — Use the callback function to filter the cells in the array

2. Description

array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )

Pass each value in the array array to the callback function in turn. If the callback function returns true, the current value of the array array will be included in the returned result array. The key names of the array remain unchanged.

3. Parameters

array: the array to be looped

callback: the callback function used

(If no callback function is provided, all entries in the array with a value equal to FALSE will be deleted. See Converting to Boolean values ​​for more information.)

flag: Determines the ## the callback receives. #Parameter form, as follows:

① ARRAY_FILTER_USE_KEY - callback accepts the key name as the only parameter

② ARRAY_FILTER_USE_BOTH - callback accepts both key name and key value

4, return value

returns the filtered array.

array_filter is actually a very easy-to-use function. Commonly used scenarios include multi-condition filtering on forms. You can directly use this function to filter out filter items without values.


But there is a problem that must be paid attention to:

array_filter will filter out any value equal to FALSE, that is to say, 0 value, empty string, null, will be filtered

When a value in your filter item is equal to 0, the problem will be exposed. In our project, we did filtering when adjusting the interface. Unexpectedly, there is a default status equal to 0. The parameters were filtered out by me, which caused the online data to be abnormal, which can be regarded as a relatively low-level error. Keep this article in mind.

————————————————

Copyright statement: This article is an original article by CSDN blogger "Good Man Ergou" and follows CC 4.0 BY- SA Copyright Agreement, please attach the original source link and this statement when reprinting.

Original link: https://blog.csdn.net/qq_39195042/article/details/82852598

Recommended tutorial: "

PHP Tutorial"

The above is the detailed content of Use of php array_filter function (detailed explanation). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete