PHP array_filter() function
In this chapter you will learn:
- Definition for PHP array_filter() function
- Syntax for PHP array_filter() function
- Parameter for PHP array_filter() function
- Example - Filter by user defined 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//j a v a 2 s.c om
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.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP array_flip() function
- Syntax for PHP array_flip() function
- Parameter for PHP array_flip() function
- Return value from PHP array_flip() function
- Example - Flip an associate array
Home » PHP Tutorial » PHP Array Functions