Re: iterating through object structure



On Wed, 31 Oct 2007 17:34:52 +0100, Good Man <heyho@xxxxxxxxxx> wrote:

JamesG <mailmehere@xxxxxxxxx> wrote 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
.



Relevant Pages

  • Re: Need Algorithm or Data Structure for Organizing "Hierarchical" Data
    ... You can iterate over it with either a depth-first ... or breadth-first search. ... multiple children; I have multiple parents; I'm completely unrelated ... delegate for each existing item in the data structure. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: More Efficient: Hashtable or List cont.....
    ... Ignacio Machin ... My app needs to potentially store a large number of custom objects and be ... Is using enumerators to iterate through the data structure a good idea? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: PEP 372 -- Adding an ordered directory to collections
    ... I think for this data structure it's important to keep all the ... My guess is that it includes it for performance reasons. ... making byindex() a method of odict, ... layer where one only has to iterate over the linked list offset times ...
    (comp.lang.python)
  • Iterating Over Dictionary From Arbitrary Location
    ... In my application I will be having several messages and my own type of IDs to go with these messages. ... Hence a dictionary seems a better choice than a list, as I will be using my own IDs to index into the data structure. ... even if it was possible to sort a dictionary, it is not possible to iterate over it from a given location. ... So, it seems I want the best of both worlds: specific indexing using my own IDs/keys (not just by list element location), sorting and the ability to start iterating from a specific location. ...
    (comp.lang.python)