Re: Filtering arrays based on an object attribute
- From: Sjoerd <sjoerd-news@xxxxxxxxxxxx>
- Date: Tue, 27 Feb 2007 23:52:35 +0100
laredotornado@xxxxxxxxxxx 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');
.
- References:
- Filtering arrays based on an object attribute
- From: laredotornado@xxxxxxxxxxx
- Filtering arrays based on an object attribute
- Prev by Date: Re: Trouble passing mysql table name to php function and using it!
- Next by Date: php website
- Previous by thread: Re: Filtering arrays based on an object attribute
- Next by thread: php website
- Index(es):
Relevant Pages
|