The array_filter() function passes each value of a given array to a user defined function. If the user defined function allows, the current value from the array is returned into the result array.
Note: The function Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from the array is returned into the result array. Array keys are preserved.
Parameter | Description |
---|---|
array | Required. Specifies the array to filter |
callbackfunction | Optional. Specifies the callback function to use |
flag | Optional. Specifies what arguments are sent to callback:
|
Syntax
array_filter(array,callbackfunction);
Example
function name($var)
{
return($var & 2);
}
$array=array("Samuel","Cooper",5,6,7);
print_r(array_filter($array,"name"));
Output
Array ( [3] => 6 [4] => 7 )
Another Example
function name($var)
{
return($var & 'Samuel');
}
$array=array("Samuel","Cooper",5,6,7);
print_r(array_filter($array,"name"));
Output
Array ( [0] => Samuel [1] => Cooper )