Php returns data as an interface-php Tutorial

Source: Internet
Author: User
Php returns data as an interface. php returns data as an interface.
{"Status": "0", "message": "\ u9a8c \ u8bc1 \ u7801 \ u9519 \ u8bef", "data ": {"a": "1"}, where $ result is an array in data. if $ result is empty, $ result = array (); output {"status ": "0", "message": "\ u9a8c \ u8bc1 \ u7801 \ u9519 \ u8bef", "data": []}. the returned data is in brackets, how to replace data with braces: {"status": "0", "message": "\ u9a8c \ u8bc1 \ u7801 \ u9519 \ u8bef", "data ": what about the form?


Reply to discussion (solution)

Generally, it is unnecessary. Otherwise, you have to write it again.

Generally, it is unnecessary. Otherwise, you have to write it again.


No way. The front-end IOS and Android devices are too busy to require this format.

The returned result is as follows:
Echo json_encode ($ array );
You changed it to this.
Echo str_replace ('[]', '{}', json_encode ($ array ));

Use the JSON_FORCE_OBJECT parameter.

 0);var_dump(json_encode($a,JSON_FORCE_OBJECT));



string(2) "{}"string(19) "{"0":1,"1":2,"2":3}"string(9) "{"ret":0}"



JSON_FORCE_OBJECT (integer)
Outputs a class (Object) instead of an array from a non-correlated array. This is especially useful when the array is empty and the receiver needs a class (Object. It takes effect from PHP 5.3.0.

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.