Let's take a look at the solution to the PHPFatalerror: CannotuseobjectoftypestdClassasarrayin error in php development. ordinary array appears... let's take a look at the solution to the php Fatal error: Cannot use object of type stdClass as array in during PHP development.
The following error occurs in a common array:
StdClass Object ([id] => 1 [title] => excellent recommendation [size] => 280*150 [pic] =>. /uploadfiles/201402160422.jpg [state] => 0) [1] => stdClass Object ([id] => 2 [title] => Enterprise News [size] => 280*150 [pic] =>. /uploadfiles/201402160533.jpg [state] => 0 ))
Baidu searched for a similar question, but the other side was json data. When php calls json_decode to generate a json object from a string object, if the [] operator is used to retrieve data, the following error is returned:
Cannot use object of type stdClass as array
The code is as follows:
$ Res = json_decode ($ res );
$ Res ['key']; // use the object after json_decode () as an array.
Solution:
1. using json_decode ($ d, true) 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. you can change it to $ res-> key.
Now we can go back to our original problem and find that [] cannot be directly used for display. the output of the above code is: $ pic [0] [title], changed to $ pic [0]-> title.
Article link:
Save this article for favorites!