0

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!!

3
  • How can we answer this if you're not showing what you are var_export() ing? Commented Aug 31, 2010 at 7:46
  • this is what is var_exported! Commented Aug 31, 2010 at 7:49
  • Pekka, it is var_export()-ed. Look in the PHP Manual, example #2. Commented Aug 31, 2010 at 7:51

4 Answers 4

1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Sorry for the delay - this worked perfectly! I didnt like using the first foreach, but hey, its working!
0

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.

1 Comment

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

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

1 Comment

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

Got a good solution to this - had a stdClass that contained other stdClases and arrays

function cleanEveryElement($someStdClass) {
    foreach ($someStdClass as &$property) {        
        if ($property instanceof stdClass || is_array($property)) {
            $property = cleanEveryElement($property);
        }
    else {
        // Perform some function on each element, eg:
        $property = trim($property);
        }
    }
return $someStdClass;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.