Function Overview
The array_filter() function is a function used to filter elements in an array. It will return a new array. The elements in the new array are filtered according to a certain condition by the elements in the original array. According to the definition of the official documentation, its syntax is as follows:
array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )
Where $array represents the original array to be filtered, $callback is an optional callback function parameter, and $flag is an optional mask parameter.
The array_filter() function in php is used to filter elements in an array and return a new array. The elements of the new array are all original array elements with the return value true. The syntax of the array_filter() function is as follows:
array_filter ( array $array [, callable $callback [, int $flag = 0 ]] ) : array
Among them, the parameters are as follows:
-
$array
: Required parameter, indicating the original array to be filtered. -
$callback
: Optional parameter, a callback function for filtering, can be a built-in function or a custom function, used to filter and judge array elements. If this parameter is omitted, all elements in the array with a value of true are returned. -
$flag
: Optional parameter, indicating the behavioral flag of the callback function, commonly used values includeARRAY_FILTER_USE_KEY
(The callback function accepts the key name of the element as a unique parameter) andARRAY_FILTER_USE_BOTH
(The callback function accepts both the key name and key value of the element as parameters). The default value is 0, that is, only the value of the element is used as a parameter.
Examples are as follows:
$array = [1, 2, 3, 4, 5]; $newArray = array_filter($array, function($value){ return $value % 2 == 0; }); print_r($newArray);
The above code will output:
Array
(
[1] => 2
[3] => 4
)
Among them, the callback function is used to determine whether the array element is even, filter out all even elements, and return to the new array.
PHP function array_filter
array_filter iterates over each element in the array to a custom function, retaining the value returning true into a new result array.
/** * Iterates over each value in the <b>array</b> * passing them to the <b>callback</b> function. * If the <b>callback</b> function returns true, the * current value from <b>array</b> is returned into * the result array. Array keys are preserved. * @link /manual/en/ * @param array $input <p> * The array to iterate over * </p> * @param callback $callback [optional] <p> * The callback function to use * </p> * <p> * If no callback is supplied, all entries of * input equal to false (see * converting to * boolean) will be removed. * </p> * @param int $flag [optional] <p> * Flag determining what arguments are sent to <i>callback</i>: * </p><ul> * <li> * <b>ARRAY_FILTER_USE_KEY</b> - pass key as the only argument * to <i>callback</i> instead of the value</span> * </li> * <li> * <b>ARRAY_FILTER_USE_BOTH</b> - pass both value and key as * arguments to <i>callback</i> instead of the value</span> * </li> * </ul> * @return array the filtered array. * @meta */ function array_filter(array $input, $callback = null, $flag = 0) { }
Example 1: The callback function is not defined by default
$array = [ 3, false, 0, '',null ]; $result = array_filter($array); var_dump($result); //result//array(1) { // [0] => // int(3) //}
Example 2: Define a function and return an element equal to 0
$array = [ 3, false, 0, '',null ]; $result = array_filter($array, function ($v){ return $v === 0; }); var_dump($result); //result//array(1) { // [2] => // int(0) //}
Example 3: Pass the third parameter and return the element with the key equal to 1
$array = [ 3, false, 0, '',null ]; $result = array_filter($array, function ($k){ return $k === 1; }, ARRAY_FILTER_USE_KEY); var_dump($result); //result//array(1) { // [1] => // bool(false) //}
Example 4: Pass the third parameter, return the element whose key is equal to 1 and whose value is equal to false
$array = [ 3, false, 0, '',null ]; $result = array_filter($array, function ($v, $k){ return $k === 1 && $v === false; }, ARRAY_FILTER_USE_BOTH); var_dump($result); //result//array(1) { // [1] => // bool(false) //}
This is the end of this article about the array_filter() function in php. For more related contents of php array_filter() function, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!