Solution to CannotuseobjectoftypestdClassasarray errors

Source: Internet
Author: User
Today, I debugged a php program and found an unexpected error. the error code is CannotuseobjectoftypestdClassasarray. What's going on? After checking for half a day, I found that this was actually a json writing problem:

Today, I debugged a php program and found an unexpected error. Error Code: Cannot use object of type stdClass as array. What's going on? After checking for half a day, I found that this was actually a json writing problem:

Cause:

$ Res = json_decode ($ res );

$ Res ['key']; // use the object after json_decode () as an array.

The following two solutions are provided on the Internet:

1. use json_decode ($ d, true ). The second variable of json_decode is set to true.

2. json_decode ($ res) returns an object. you cannot use $ res ['key'] for access. you can replace it with $ res-> key.

Reference Manual: json_decode
Return Values: Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned.

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.