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.
PHP array_filter() function has the following syntax.
array array_filter ( array arr [, function callback] )
Parameter | Is Required | Description |
---|---|---|
arr | Required. | Array to filter |
callback | Required. | Callback function |
Filter by user defined function
<?PHP
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.