Filter the values of an array using a callback function:
<?php
function test_odd($var)
{
return($var & 1);
}
$a1=array(1,3,2,3,4);
print_r(array_filter($a1,"test_odd"));
?>
Try it Yourself »
The array_filter() function filters the values of an array using a callback function.
This 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.
array_filter(
array, callbackfunction, flag)
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:
|
Return Value: | Returns the filtered array |
---|---|
PHP Version: | 4.0.6+ |
PHP Changelog: | PHP 5.6: Added optional flag parameter |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!