views:

141

answers:

3

This is the print_r() version of a data structure that I need to access via a foreach loop:

stdClass Object
(
    [DetailedResponse] => Array
        (
            [0] => stdClass Object
                ( ...
                )

            [1] => stdClass Object
                ( 
...

Now, how do I iterate though these objects?

I can sense that I should be doing something like this:

$object->DetailedResponse[0];
$object->DetailedResponse[1];

But how do I put it in a foreach type loop!!

A: 

seems like there are multiple objects in that obj.. you might need to do more foreach loops.. this code should get you the first sessionId in that obj.

foreach ($detailedresponses as $detailedresponse) {
    foreach ($detailedresponseas as $response) {
        echo $response->sessionId;

    }
}

run this code to see the obj in a clearer way:
echo '<pre>'; print_r($detailsresponses); exit;

replace '$detailedresponses' with your correct variable name and post it back here, it should make things easier to read.

EDIT
check out this URL, I put my test data in there: http://pastie.org/1130373

I recreated the object you're getting and put comments in there so you can understand what's happening :)

AND, you can get the properties like this:

echo $object->DetailedResponse[0]->sessionId;
Maikel
Hi - I was able to get a cleaner picture ... see edit above...
tzmatt7447
Maikel - please have a look at the edit above which shows the object proparly..
tzmatt7447
Thanks! Sorry for the delay - this worked perfectly! I didnt like using the first foreach, but hey, its working!
tzmatt7447
A: 

very simple. you have a so called standard-object of php. it's accessable like any other object in php by the $object->property syntax

so you can iterate over it this way: foreach($object as $property), or foreach($object as $prop_name => $prop_val) where you can access the properties by $object->$prop_name.

helle
Hi helle - could you have a look at the specifix of the issue i'm facing?
tzmatt7447
A: 

If you want to save a class, for re-using it later, you'd better to use serialize and unserialize()

Lekensteyn
This comment gives a reason for using serialize/unserialize over var_export: http://nl2.php.net/manual/en/function.var-export.php#94333
Lekensteyn