Black Friday 50% off every Pro template with BLACKFRIDAY50, added on the payment page · ends in 3 days See the sale →

3 easy ways to fix “Cannot use object of type stdclass as array” error

3 easy ways to fix “Cannot use object of type stdclass as array” error

Are you looking for a useful solution to handle the “Cannot use object of type stdclass as array” error? If yes, look no further. In this blog, we will demonstrate 3 efficient solutions that may help you tackle this error. Now, let’s check them out.

Why did the “Cannot use object of type stdclass as array” appear on your website?

The error message “Cannot use object of type stdclass as array” is a common error in PHP programming.

This error message appears when you try to access an object property as if it were an array element, but the object is actually an instance of the stdClass class, which is a generic class in PHP used to create objects without a specific class definition. In other words, you’re trying to treat an object like an array, but it’s not an array, it’s an object.

Cannot use object of type stdclass as array

How to address “Cannot use object of type stdclass as array” error?

In order to handle this error, you can refer to one of the following solutions:

Solution 1: Convert the object to an associative array

You can convert the object of type stdClass to an associative array by passing true as the second argument to the json_decode() function. This will return an associative array instead of an object, which you can then pass to the print_r() function.

Example code:

$json_string = '{"key1": "value1", "key2": "value2"}';
$assoc_array = json_decode($json_string, true);
print_r($assoc_array);

Solution 2: Use var_dump() instead of print_r()

The var_dump() function is designed to output information about PHP variables, including objects. You can use var_dump() to inspect the object of type stdClass.

Example code:

$json_string = '{"key1": "value1", "key2": "value2"}';
$obj = json_decode($json_string);
var_dump($obj);

Solution 3: Convert the object to a JSON string using json_encode()

You can use the json_encode() function to convert the object of type stdClass to a JSON string, which you can then pass to the print_r() function.

$json_string = '{"key1": "value1", "key2": "value2"}';
$obj = json_decode($json_string);
$json_string2 = json_encode($obj);
print_r($json_string2);

Conclusion

Hopefully, these mentioned solutions may help you handle your error effectively. If your error doesn’t disappear, please let us know your situation in the comment below. We will support you soon.

Additionally, you can visit our website to discover a collection of stunning, eye-catching, free WordPress Themes.

Build your next site faster.

Every Engine Templates design has a free version, and Pro comes with the complete demo site, ready to import.

Keep reading

Related articles.

Free download

We'll send the link, setup tips and update notices. Help is in the documentation and our contact form.

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