473,499 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

iterating through object structure

Hi,

I have a data structure as follows (print_r($data)):

Array
(
[0] =stdClass Object
(
[FIELD1] =2355
[FIELD2] =123
[FIELD3] =1.25
)

[1] =stdClass Object
(
[FIELD1] =309
[FIELD2] =122
[FIELD3] =0.55
)

)

I can't seem to iterate through it,

If I try
foreach($data as $obj) {
echo $obj->FIELD1 + $obj->FIELD2;
}

It only shows the first object, not the rest. What am I doing wrong?
Thanks

Oct 31 '07 #1
2 3234
JamesG <ma********@gmail.comwrote in news:1193843101.021717.143420@
19g2000hsx.googlegroups.com:
Hi,

I have a data structure as follows (print_r($data)):

Array
(
[0] =stdClass Object
(
[FIELD1] =2355
[FIELD2] =123
[FIELD3] =1.25
)

[1] =stdClass Object
(
[FIELD1] =309
[FIELD2] =122
[FIELD3] =0.55
)

)

I can't seem to iterate through it,

If I try
foreach($data as $obj) {
echo $obj->FIELD1 + $obj->FIELD2;
}

It only shows the first object, not the rest. What am I doing wrong?
Using a '+' instead of a '.' ?

Oct 31 '07 #2
On Wed, 31 Oct 2007 17:34:52 +0100, Good Man <he***@letsgo.comwrote:
JamesG <ma********@gmail.comwrote in news:1193843101.021717.143420@
19g2000hsx.googlegroups.com:
>Hi,

I have a data structure as follows (print_r($data)):

Array
(
[0] =stdClass Object
(
[FIELD1] =2355
[FIELD2] =123
[FIELD3] =1.25
)

[1] =stdClass Object
(
[FIELD1] =309
[FIELD2] =122
[FIELD3] =0.55
)

)

I can't seem to iterate through it,

If I try
foreach($data as $obj) {
echo $obj->FIELD1 + $obj->FIELD2;
}

It only shows the first object, not the rest. What am I doing wrong?

Using a '+' instead of a '.' ?
I wouldn't expect so, that would still display the 2 different sums
(allthough it will seems like one string).

Can't say I can recreate it, this:
$a = array(new stdClass(),new stdClass());
$a[0]->FIELD1 = 1;
$a[0]->FIELD2 = 2;
$a[1]->FIELD1 = 3;
$a[1]->FIELD2 = 4;
foreach($a as $o){
echo $o->FIELD1 + $o->FIELD2;
}
?>
Will result in an output of '37', which is what's expected.
--
Rik Wasmus
Oct 31 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4526
by: Fernando Rodríguez | last post by:
Hi, While iterating through a list I'd like to know not just the current element, but also its index. Is there a better way than this: i = 0 newList = for element in aList:...
34
4129
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
7
1682
by: Dave Hansen | last post by:
OK, first, I don't often have the time to read this group, so apologies if this is a FAQ, though I couldn't find anything at python.org. Second, this isn't my code. I wouldn't do this. But a...
6
6027
by: Gustaf Liljegren | last post by:
I ran into this problem today: I got an array with Account objects. I need to iterate through this array to supplement the accounts in the array with more data. But the compiler complains when I...
3
2637
by: Bob Day | last post by:
Using VS 2003, VB.NET, MSDE Is there a way to itterate through the items in a structure? Consider the structure below, which holds the name of wave files. I would like to itterate through them...
6
2032
by: Ken Fine | last post by:
I'm using SQLDataSource, which generates some kind of dataset, and then I attach that datasource to various data display controls such as DataList and repeater which loop through to the end of the...
5
1950
by: mikehulluk | last post by:
Ok, Imagine I have a class class C { }; ostream& operator<<(ostream& o, const C& c) { ...}
0
1046
by: andi23coe | last post by:
Hello everybody! I have a problem with listing the properties of an unknown class via reflection so far. My class structure, automatically generated through linq to xsd: meeting --> VNR...
7
5445
by: digital.pardoe | last post by:
Hi, I have had a little programming experience with C but not enough to accomplish what I am trying to do. Nor have I been able to find any references on the internet regarding what I am trying...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5479
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4920
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.