PHP array_filter() function

Example

Filter elements in the array using a callback function:

<?php
function test_odd($var)
{
return($var & 1);
}
$a1=array("a","b",2,3,4);
print_r(array_filter($a1,"test_odd"));
?>

Run Example

Definition and Usage

The array_filter() function filters the values in the array using a callback function.

This function passes each key-value pair of the input array to the callback function. If the callback function returns true, then the current key-value pair of the input array is returned in the result array. The array keys remain unchanged.

Syntax

array_filter(array,callbackfunction);
Parameter Description
array Required. Specifies the array to filter.
callbackfunction Required. Specifies the callback function to use.

Technical Details

Return value: Return filtered array.
PHP Version: 4.0.6+