Hudzilla.org - the homepage of Paul Hudson
Contents > Arrays > Array-specific functions Wish List | Report Bug | About Me ]

5.6.3     Filtering your array through a function: array_filter()

This is NOT the latest copy of this book; click here for the latest version.

array array_filter ( array input [, callback function])

The final array function in this group is array_filter(), which is a very powerful function that allows you to filter elements through a function you specify. If the function returns true, the item makes it into the array that is returned, otherwise it is not. Consider this script:

<?php
    
function endswithy($value) {
        return (
substr($value, -1) == 'y');
    }

    
$people = array("Johnny", "Timmy", "Bobby", "Sam", "Tammy", "Danny", "Joe");
    
$withy = array_filter($people, "endswithy");
    
var_dump($withy);
?>

In this script we have an array of people, most of which have a name ending with "y". However, several do not, and for one reason or another we want to have a list of people whose names ends in "y", so array_filter() is used. The function endswithy() will return true if the last letter of each array value ends with a y, otherwise false. By passing that as the second parameter to array_filter(), it will be called once for every array element, passing in the value of the element as the parameter to endswithy(), where it is checked for a "y" at the end.





<< 5.6.2 Stripping out duplicate values: array_unique()   5.6.4 Converting an array to individual variables: extract() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Just Me - 06 Sep 2008

Chima & The Author are right, works this way 4 me 2

chima/chytons@yahoo.com - 06 Sep 2008

Zameer, you're wrong. The author is right.

chima/chytons@yahoo.com - 06 Sep 2008

Zameer, you're wrong. The author is write.

A PHP User - 06 Sep 2008

^ same difference

Zameer - 06 Sep 2008

Incorrect: $withy = array_filter($people, "endswithy");
Correct: $withy = array_filter($people, "endswithy()");



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is three plus five?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow