How to Fix the Uncaught Error "Cannot Use Object of Type stdClass as an Array"

The given snippet is dedicated to a common PHP issue "cannot use object of Type stdClass as an array".

Read on to learn how to fix this issue accurately.

A. Use json_decode

The function that we recommend you to use is json_decode.

This function is capable of returning an object by default.

Data may be accessed in the following way:


In the event of having identifiers such as, you should implement:


In case you intend to have an array, then run the code below:

$result = json_decode($json, true);

Alternatively, you can cast the object to an array like so:

$result = (array) json_decode($json);

B. Accessing with ->

Let’s consider another solution with the -> sign.

So, access with the help of -> as it is an object.

Then, change the code from:




There you are.

Description of the stdClass in PHP

This is considered an empty class in PHP. It is applied for casting other types to the object. The stdClass has similarities with Python or Java objects.

You should take into consideration that stdClass is not the base class. Once an object is converted to an object, it is not changed. Yet, once an object type is converted to an instance of the stdClass generated, it is not NULL. Once it is NULL, the new instance will be empty.

The main use cases of the stdClass in PHP are:

  • It directly accesses the members by calling them.
  • It is handy for dynamic objects.
  • It is used for setting dynamic properties, and so on.

About json_decode in PHP

The json_decode function is used for taking a JSON encoded string and converting it into a PHP variable.

It has four parameters: json, assoc, depth, and options.

Once the assoc parameter is TRUE, then the returned objects will be converted to associative arrays.