Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP array_filter() Function

PHP array_filter() Function

PHP Array ReferenceComplete PHP Array Reference Manual

Example

Filter the elements of an array using a callback function:

Run Example »


Definition and Usage

The array_filter() function filters the elements of an array using a callback function.

This function passes each value of the input array to the callback function. If the callback returns true, the current value of the input array is added to the result array. Array keys are preserved.


Syntax

array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )
Parameters Description
array Required. Specifies the array to filter.
callback Optional. Specifies the callback function to use.
flag

Optional. Determines which arguments the callback receives:

  • ARRAY_FILTER_USE_KEY - callback receives the key as its only argument
  • ARRAY_FILTER_USE_BOTH - callback receives both the key and the value
  • |

    Technical Details

    Return Value: Returns the filtered array.
    PHP Version: 4.0.6+

    PHP Array ReferenceComplete PHP Array Reference Manual