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/*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.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions