PHP Tutorial - 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

ParameterIs RequiredDescription
arrRequired.Array to filter
callbackRequired.Callback function

Example

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.