Contents
How to fix the cannot use object of type stdClass as array error?
How many times have you fallen upon the dreaded “ cannot use object of type stdClass as array” error message? In this tutorial, you will learn how to solve the error and it’s much easier than you think.
Why do I get error cannot use stdClass as array in PHP?
The PHP engine throws the “ cannot use object of type stdClass as array” error message due to your code trying to access a variable that is an object type as an array. It is most likely that you’ve tried to access the data with the generic bracket array accessor and not an object operator. Always ensure that you know the variable type
Can a result be a STD object in PHP?
Otherwise $result will be an std object. but you can access values as object. Sometimes when working with API you simply want to keep an object an object. To access the object that has nested objects you could do the following: The expected output would be “Robert” with a line break.
Is the discogsjson object an array or a stdClass?
$discogsJSON is not an array, but an stdClass object. results is a property on this object. This should do what you are looking for. However, it may be more convinient to add a true flag to json_decode to make sure that the returned value is an associative array.
What is stdClass and how to use it?
I don’t know what stdClass is and how to use it. stdClass is a class which creates an instance of simple object. Properties of classes are to be accessed using ->, not […] notation.
What does undefined mean on a stdClass object?
“Undefined property” means that your object doesn’t have an “order” property. I’m guessing your $pages_item is being returned from your database, so you either don’t have have an “order” column, or you’re not setting it when you’re creating the $pages_item object.
Can a class be accessed as an array?
Properties of classes are to be accessed using ->, not […] notation. As per documentation for json_decode, simply setting the second argument to true will result in the result being associative array, which can be in turn accessed like an array.