Definition
The array_filter() function passes each value of the input array to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.
Syntax
array_filter(array, callbackfunction, flag)
Parameters
| 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: ARRAY_FILTER_USE_KEY - pass key as the only argument to callback (instead of the value); ARRAY_FILTER_USE_BOTH - pass both value and key as arguments to callback (instead of the value) |
Example
<?php
function testEven($num){
if($num % 2 == 0){
return true;
}
}
$numbers = array("a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 5);
$result = array_filter($numbers, "testEven");
print_r($result);