iterating through object structure

iterating through object structure

am 31.10.2007 16:05:01 von JamesG

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

Re: iterating through object structure

am 31.10.2007 17:34:52 von Good Man

JamesG 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 '.' ?

Re: iterating through object structure

am 31.10.2007 17:50:08 von luiheidsgoeroe

On Wed, 31 Oct 2007 17:34:52 +0100, Good Man wrote:

> JamesG wrote in news:1193843101.021717.143420@
> 19g2000hsx.googlegroups.com:
>
>> Hi,
>>
>> I have a data structure as follows (print_r($data)):
>>
>> Array
>> (
>> [0] =3D> stdClass Object
>> (
>> [FIELD1] =3D> 2355
>> [FIELD2] =3D> 123
>> [FIELD3] =3D> 1.25
>> )
>>
>> [1] =3D> stdClass Object
>> (
>> [FIELD1] =3D> 309
>> [FIELD2] =3D> 122
>> [FIELD3] =3D> 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 =3D array(new stdClass(),new stdClass());
$a[0]->FIELD1 =3D 1;
$a[0]->FIELD2 =3D 2;
$a[1]->FIELD1 =3D 3;
$a[1]->FIELD2 =3D 4;
foreach($a as $o){
echo $o->FIELD1 + $o->FIELD2;
}
?>
Will result in an output of '37', which is what's expected.
-- =

Rik Wasmus