p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Examples of the array_filter function


Message #1 by =?iso-8859-1?q?Oonagh=20Molloy?= <oonaghmolloy2002@y...> on Tue, 17 Sep 2002 08:55:48 +0100 (BST)
Hello all. This is my first post and it's just a
general query - so don't go mad, if you can't comment.
I was recently trying to use the array_filter function
on an application I was producing.

I was disappointed to find that the example of its use
that is to be found in the O'Reilly book "PHP
Programming" is a blatantly secondrate attempt at
customising the example that is available to one and
all in the Zend website's function library
(http://www.zend.com/manual/function.array-filter.php...
and mirrored in no end of languags, everywhere else on
the web): filter out all the array elements with an
even index number. I could indulge in a lavish rant,
at this point, about how pointless it seems to me, to
wrap up someone else's function to make it look like
your own, print it on a page and then charge good
money for it, but I won't :).

Wrox's "Beginning PHP4" seems not to cover this
function, nor (from my initial perusal of the book's
index, at least) does "Professional PHP4". Nor, more
alarmngly, does the PHP Function Reference at the P2P
website (it's here:
http://p2p.wrox.com/content/phpref/). I'd try the
Sam's book, but I'm running out of money! Is this a
'non-standard' function?

As a caveat, I'll add that, in the end, I used a
completely different approach to building my
application which didn't require array filtering and
is probably quicker (and yet again proves the
flexibility of this language).
However, I was wondering if there were any examples of
how to use this function, or any advice about whether
it isn't widely supported, or whatever. In principle,
it seems like it would be useful.

Regards all,
Oonagh Molloy

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com
Message #2 by "Nikolai Devereaux" <yomama@u...> on Tue, 17 Sep 2002 09:21:46 -0700
It's a fair question.  I can't say that I've ever had to use array_filter.
The reason is probably because most of the arrays I assemble are assembled
with the contents I want.  If I generate an array from some rows in the
database, for example, I perform the equivalent of array_filter() by using a
WHERE clause in my SELECT query.

Don't get me wrong -- the use of callback functions is extremely useful,
though.  Especially when sorting arrays...


if you would like an example of array_filter(), here ya go:

function is_male($person)
{
  return strcasecmp($person['gender'], 'male') == 0;
}

// get_people returns a numerically indexed array, where each index
// in the array represents a person.  Each person is a string-indexed
// array, with the following indexes set:
// name, dob, gender, email, height, weight, eyes, hair

$people = get_people();

$males = array_filter($people, 'is_male');
$females = array_diff($people, $males); // freebie example!!


take care,

nik

Message #3 by "Oonagh Molloy" <oonaghmolloy2002@y...> on Fri, 20 Sep 2002 10:44:41
Many thanks, Mr Deveraux! My appologies for not replying sooner (I was 
called away down to my unle's, where they've ony just got running water 
and the wireless, so I couldn't get online :). 
 
I think an example of filtering an ASSOCIATIVE array was exactly what I 
was after. I see array_filter being useful because it could save 
performing a second database query - effectively running a second WHERE 
on what you've got without doing the database legwork. That said, my poor 
brain cannot now furnish me with the reason I was trying to use it in the 
first place, so maybe you are right, and there is usually a better way of 
doing things!  
  
I'm a bit new to all this programming lark. I used to be a Mac 'power  
user' - and was happy, until my husband let on to me that I had somehow  
magically become a Unix user while I wasn't paying attention! Eh? How did  
that happen? The next thing I know, and it's "terminal this" and 
"terminal that", with a copy of "Unix in a nutshell" gogglin' at me all 
the while... Jesus, "emacs on and iMac"! Welcome to the 21st Century, 
please form an orderly queue! :)  
  
Warm regards from a somewhat soft mornin' in Mayo  
Oonagh  
  
>   
It's a fair question.  I can't say that I've ever had to use  
array_filter.  
The reason is probably because most of the arrays I assemble are  
assembled  
with the contents I want.  If I generate an array from some rows in the  
database, for example, I perform the equivalent of array_filter() by  
using a  
WHERE clause in my SELECT query.  
  
Don't get me wrong -- the use of callback functions is extremely useful,  
though.  Especially when sorting arrays...  
  
  
if you would like an example of array_filter(), here ya go:  
  
function is_male($person)  
{  
  return strcasecmp($person['gender'], 'male') == 0;  
}  
  
// get_people returns a numerically indexed array, where each index  
// in the array represents a person.  Each person is a string-indexed  
// array, with the following indexes set:  
// name, dob, gender, email, height, weight, eyes, hair  
  
$people = get_people();  
  
$males = array_filter($people, 'is_male');  
$females = array_diff($people, $males); // freebie example!!  
  
  
take care,  
  
nik  
  

  Return to Index