PHP JSON Error: Cannot use object of type stdClass as array Solution

  • 2021-07-13 04:40:52
  • OfStack

When php calls json_decode again to generate an json object from a string object, if you use the [] operator to fetch data, you get the following error:


Cannot use object of type stdClass as array

Causes:

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

Solutions (2 kinds):

1. Use json_decode ($d, true). Is to set the second variable of json_decode to true.
2. json_decode ($res) returns an object that cannot be accessed using $res ['key']. Instead, $res- > key will do.


Related articles: