Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old February 27th, 2007, 10:35 PM
laredotornado@zipmail.com
Guest
 
Posts: n/a
Default Filtering arrays based on an object attribute

Hi,

I have an array filled with a particular type of object, which
contains an attribute "m_level", of integer type. What I want is to
get a subset of the array whose "m_level" attribute is equal to zero.
Is there a short way I can do this other than iterating through a
foreach / for loop?

I'm using PHP 4.4.4.

Thanks, - Dave

  #2  
Old February 27th, 2007, 10:55 PM
OmegaJunior
Guest
 
Posts: n/a
Default Re: Filtering arrays based on an object attribute

On Tue, 27 Feb 2007 23:23:14 +0100, laredotornado@zipmail.com
<laredotornado@zipmail.comwrote:
Quote:
Hi,
>
I have an array filled with a particular type of object, which
contains an attribute "m_level", of integer type. What I want is to
get a subset of the array whose "m_level" attribute is equal to zero.
Is there a short way I can do this other than iterating through a
foreach / for loop?
>
I'm using PHP 4.4.4.
>
Thanks, - Dave
>
Did you try the array_filter() function?


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
  #3  
Old February 27th, 2007, 11:05 PM
Sjoerd
Guest
 
Posts: n/a
Default Re: Filtering arrays based on an object attribute

laredotornado@zipmail.com wrote:
Quote:
Hi,
>
I have an array filled with a particular type of object, which
contains an attribute "m_level", of integer type. What I want is to
get a subset of the array whose "m_level" attribute is equal to zero.
Is there a short way I can do this other than iterating through a
foreach / for loop?
>
I'm using PHP 4.4.4.
>
Thanks, - Dave
>

function filter_particular_type($array) {
$result = array();
foreach ($array as $item) {
if ($item->m_level == 0) {
$result[] = $item;
}
}
return $result;
}

// or

function callback_filter($item) {
return $item->m_level == 0;
}
array_filter($array, 'callback_filter');
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles