PHP array_filter() function
Definition
The array_filter()
filters elements through a function you specify.
If the function returns true, the item makes it into the array; otherwise, it does not.
Syntax
PHP array_filter() function has the following syntax.
array array_filter ( array arr [, function callback] )
Parameter
Parameter | Is Required | Description |
---|---|---|
arr | Required. | Array to filter |
callback | Required. | Callback function |
Example
Filter by user defined function
<?PHP/*from w w w. j a v a 2 s . com*/
function endswithy($value) {
return (substr($value, -1) == 'y');
}
$people = array("Johnny", "Funny", "java2s.com", "Sunny", "Bunny", "Cunny");
$withy = array_filter($people, "endswithy");
var_dump($withy);
?>
The code above generates the following result.