You are here: Re: Filtering arrays based on an object attribute « PHP Programming Language « IT news, forums, messages
Re: Filtering arrays based on an object attribute

Posted by Sjoerd on 02/27/07 22:52

laredotornado@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');

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация