SoFunction
Updated on 2025-04-09

PHP JSON error: Cannot use object of type stdClass as array solution

When php calls json_decode to generate json objects from string objects, if you use the [] operator to get data, you will get the following error:

Copy the codeThe code is as follows:

Cannot use object of type stdClass as array

Cause:
Copy the codeThe code is as follows:

$res = json_decode($res);
$res['key']; //Use the object after json_decode() as an array.

Solution (2 types):

1. Use json_decode($d, true). It is to set the second variable of json_decode to true.
2. json_decode($res) returns an object. You cannot use $res['key'] for access. Just change it to $res->key.