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 face. 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 utilize json_decode(); function, it returns an object of type stdClass. Whereas, the arguments which are to be passed inside of print_r() should either be a string or an array. Therefore, once you pass an object inside of print_r() , then this error will come in PHP.

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

Cannot Use Object Of Type Stdclass As Array

How to tackle 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: By accessing the key of the Object

As we revealed above, once you utilize json_decode(); function, you will get an Object of stdClass as return type. So, you are able to access the elements of the object thanks to the assistance of -> Operator.

You need to implement 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 also set the change 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 the solution that is useful for your error by leaving a comment below. In case, your error is not disappearing, don’t hesitate to share with us your situation. 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 your reading.

5/5 - (1 vote)
Lt Digital Team (Content &Amp; Marketing)
Latest posts by LT Digital Team (Content & Marketing) (see all)

The Big Summer Sale! Get 50% Off for everything on today, don't miss it. Coupon code: SUMMER50 Redeem Now
The Big Summer Sale! Get 50% Off for everything on today, don't miss it. Coupon code: SUMMER50 Redeem Now