Cannot use object of type stdclass as array

Suddenly, one day you get into trouble with the fatal error “Cannot use object of type stdclass as array”. And you are also looking for a useful solution to handle this problem. Luckily, in this blog, we are going to…

Cannot use object of type stdclass as array

Suddenly, one day you get into trouble with the fatal error “Cannot use object of type stdclass as array”. And you are also looking for a useful solution to handle this problem. Luckily, in this blog, we are going to explain and fix this common error that every programmer faces. Before going over the solutions, you need to understand why this error arises.

Why did you hit the error “cannot use object of type stdclass as array”?

The error “cannot use object of type stdclass as array” occurs because once you use json_decode(); function, it returns an object of type stdClass. However, the arguments passed inside of print_r() should either be a string or an array. Therefore, once you pass an object inside of print_r(), this error will occur in PHP.

Now, let’s refer to the following solutions to handle this problem.

Cannot use object of type stdclass as array

How to tackle the fatal error “Cannot use object of type stdClass as array” in PHP

Here are the two solutions that can help you resolve your issue:

Method 1: Cast the object to an array

Now, you need to cast the object to an array as below:

$a = (array)$object;

Method 2: Access the keys of the object

As explained above, once you use json_decode(); function, you will get a stdClass object as the return type. So, you are able to access the elements of the object with the help of the -> Operator.

Do it as follows:

$value = $object->key;

In case the object has plenty of nested arrays, you can also utilize different keys to extract the sub-elements. For example:

$value = $object->key1->key2->key3...;

Alternatively, you can also set the second parameter of the json_decode(); to true, then the object will be automatically converted to an array();.

Also, if you set the second parameter of the json_decode(); to true, it will automatically convert the object to an array();.

Wrap up

Has your error been resolved? If yes, let us know which solution worked for you by leaving a comment below. If your error has not disappeared, don’t hesitate to share your situation with us. We are always willing to support you.

Last but not least, our website specializes in providing plenty of stunning, SEO-friendly free WordPress themes. You can visit and get one to make your site more appealing. Thanks for reading.

Build it with a template

Start from a finished design: Joomla templates, WordPress themes or the AI-ready block themes. Every template has a free version and a live demo.

Free download

We'll send the link, setup tips and update notices, and you can reach us anytime through our AI assistant and contact form.

The download starts at once. We email you the link and ask you to confirm your address. Privacy policy.