Let's look at the solution to the problem of PHP Fatal error:cannot use object of type StdClass as array error in PHP development.
The following error occurred with the normal array
| The code is as follows |
|
|
<?php
Array (
[0] => StdClass Object (
[ID] => 1
[Title] => Wonderful 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)
)
?> |
Then Baidu searched for a question about the same, but the other side was JSON data
When PHP calls Json_decode to generate a JSON object from a string object, if you use the [] operator to fetch the data, you get the following error:
Cannot use object of type StdClass as array
Causes:
| The code is as follows |
|
|
$res = Json_decode ($res);
$res [' key ']; Use the object after Json_decode () as an array. |
Solution (2 kinds):
1. Use Json_decode ($d, True). is to make the second variable of Json_decode set to true.
2, Json_decode ($res) Returns an object, can not use the $res [' key '] for access, replaced $res->key on it.
All right, now back to our original problem.
The original is not directly with [] to show the resulting, the above code output is used: $pic [0][title], changed to $pic[0]->title after normal