473,320 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

Feb 27 '07 #1
2 4484
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: elora_c | last post by:
I'm trying to write XSLT that will filter out an XML based on an attribute's value. XML looks like: <postings> <channel name="A"> <posting Connected="True" name="Posting1" /> <posting...
3
by: Alex Ayzin | last post by:
Hi, I have a problem that might be easy to solve(possibly, I've just overlooked an easy solution). Here we go: I have a dataset with 2 datatables in it. Now, I need to do the following: if...
3
by: Jason | last post by:
I am trying to filter records in a primary form based on records in related tables. The data in the related tables is being displayed in the primary form through subforms. To be more specific, I...
0
by: Patrick | last post by:
I'm working on a contact management application, and need a hand with one aspect... Here's what I want to create: ------------------------------------ A form split into two parts. There is a...
3
by: parrot toes | last post by:
Summary: I have been trying to make requests of a web service provided by Axis using a dotnet client with code generated by wsdl.exe and have been getting exceptions when trying to process the...
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
2
by: sp | last post by:
hai i am new to this XSTL and i am trying to write xslt that will filter out the xml value based on Attribute Value. my xml looks like this <catalog> <cd> <title year="1990">Empire...
5
by: Sadeq | last post by:
Is it possible to define custom attributes for arrays? And if so, how can I retrieve them? I mean I want to define sth like: int MyArray; and then retrieve the value of the custom...
10
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.