PHP array_filter() Function
Last Updated : 17 Mar 2025
The array_filter( ) function filters the values of an array using a callback function. This function passes each value of the input array to the callback function. If the callback function returns true, the current value from the input is returned into the result array. Array keys are preserved. This function was introduced in PHP 4.0.6.
Syntax
Parameter
| Parameter | Description | Is compulsory |
|---|---|---|
| array | It refers to the input array on which the filter operation is to be performed. | compulsory |
| Callback function | The callback function to use. If the function is not supplied, all entries of the array equal to false will be removed. | Optional |
| flag | Flag determines what arguments are sent to callback. ARRAY_FILTER_USE_KEY: It passes key as the only argument to callback instead of the value. ARRAY_FILTER_USE_BOTH: It Passes both value and key as arguments to callback instead of the value. | Optional |
Return Type
The function returns a filtered array.
EXAMPLE 1
Output:
Array ( [0] => javatpoint [2] => 10 [5] => 2020 [7] => 1 )
EXAMPLE 2
Output:
Array ( [1] => 2 [2] => 6 [4] => 10 )
EXAMPLE 3
Output:
Array ( [sachin] => 200 [yuvraj] => 125 )
EXAMPLE 4
Output:
Array ( [0] => 1 [2] => 3 [4] => 5 [6] => 7 [8] => 9 ) Array ( [1] => 2 [3] => 4 [5] => 6 [7] => 8 )
Next TopicPhp-array-flip-function