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!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.