PHP array_filter() Function

PHP array_filter Function


Description :

array_filter() function is used to apply a filter on array elements based on the function and returns the array with filtered elements, it accepts an array to be checked and a callback function. The callback function is used to validate the elements in the array.

Syntax

 array_filter(array, callbackfunction, flag)

Parameters

The array_filter() function accepts the following parameters.

Parameter Description
array Required. Specifies the array to filter.
callback Optional. Specifies the name of the callback function.
flag

Optional. Specifies what arguments are sent to callback:

  • ARRAY_FILTER_USE_KEY - pass key as the only argument to callback instead of value.
  • ARRAY_FILTER_USE_BOTH - pass both value and key as arguments to callback instead of the value.

Default is 0 which will pass value as the only argument to callback function.

 

Given below example Find the positive number from given array of the numbers:

Output :

Array
(
    [0] => 12
    [1] => 30
    [4] => 75
)