Home >Backend Development >PHP Tutorial >PHP Fatal error: Cannot use object of type stdClass as array in错误_PHP教程
Let’s take a look at PHP Fatal error: Cannot use object of type stdClass encountered in PHP development The solution to the as array in error problem.
The following error occurs with ordinary arrays
The code is as follows | |||||
Array (
|
Later, Baidu searched for a similar question, but the other party was json data
When PHP calls json_decode to generate a json object from a string object, if you use the [] operator to get data, you will get the following error:
Cannot use object of type stdClass as array
Cause:
The code is as follows | |||||
$res = json_decode($res);
|
Solutions (2 types):
1. Use json_decode($d, true). Just set the second variable of json_decode to true.
2. json_decode($res) returns an object and cannot be accessed using $res['key']. Just replace it with $res->key.
Okay, now back to our original problem discovery
It turns out that the problem is that [] cannot be used directly to display. The output of the above code is: $pic[0][title]. After changing it to $pic[0]->title, it becomes normal