0

Hi all,

I have an array of objects that looks like this:

object(stdClass)[2]
  public 'statuses' => 
    array (size=97)
      0 => 
        object(stdClass)[3]
          public 'metadata' => 
            object(stdClass)[4]
                public 'iso_language_code' => string 'en' (length=2)
          public 'id' => int 700797209428668416
      1 => ...
      n => ...

I a simply trying to iterate through the array of objects and access the 'id' property. The code I am using is:

foreach($obj_array as $statuses)
            {
                foreach($statuses as $s)
                    {   
                        //Store all IDs in Array
                        $id_array[] = $s->id;
                    }
            }

However, I am getting the error: Undefined property: stdClass::$id

I'm very new to working with classes, so I suspect I've just fundementally mis-understood how the data is structured.

I thought the 'id' property may be a property of 'metadata' but even $id_array[] = $s->metadata->id; gave me the same error.

Any help would be appreciated!

Edited by James_43: fixed mistake

1
Contributor
1
Reply
7
Views
1 Year
Discussion Span
Last Post by James_43
0

If I extend the foreach to the below:

foreach($response as $statuses)
            {
                foreach($statuses as $s)
                    {
                        foreach($s as $key => $a)
                        {
                            echo '<pre>';
                        var_dump($key);
                        echo '<pre>';

                        //Store all IDs in Array
                        $id_array[] = $a->id;
                        }
                    }
                }

I get the same error, but var dump shows:

string 'metadata' (length=8)
string 'id' (length=2)

So I cannot understand why $s->id isn't accessing the id property.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.