PHPFatalerror: CannotuseobjectoftypestdClassasarrayin error

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.