PHP stdClass Object within array

I have no idea what the stdClass Object is. I googled it but couldn’t find anything really informative about the stdClass.A Few articles on stackoverflow explains the basic idea behind stdClass but that’s it. I haven’t searched the official documentation though.

Anyways, this article is not about stdClass Object. It is about retrieving values from array whose members are of type stdClass.

Here is a link to official php array documentation :

Now if you print_r the simple multidimensional array It would look something like this.

Example array :

print_r($array)  of the above will give the following result.

Now if you want to access the value from “array” you can do that fairly easily with following code.

That is simple enough.

But sometimes you may also encounter an array which would look like this.

Now if you look at the array elements in the first dimension,they are of type stdClass Object. You can not just retrieve the value like we did before in the example.

Syntax to retrieve the values from the array similar to shown above is $array[0]->KEY.

We can also loop over array objects like this

Complete Example :

Output :


Categories: PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.