By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Filtering arrays based on an object attribute

P: n/a
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

Feb 27 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Tue, 27 Feb 2007 23:23:14 +0100, la***********@zipmail.com
<la***********@zipmail.comwrote:
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/
Feb 27 '07 #2

P: n/a
la***********@zipmail.com wrote:
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');
Feb 27 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.