PHP as an interface to return data related issues

Source: Internet
Author: User
PHP as an interface to return data problems
PHP as an interface to return data problems
When there is data, the return is {"status": "0", "message": "\U9A8C\U8BC1\U7801\U9519\U8BEF", "data": {"a": "1"}}, where data is the array $result, if $ If result is empty, $result = Array (); Output {"status": "0", "message": "\U9A8C\U8BC1\U7801\U9519\U8BEF", "Data": []}, the data returned is the brackets, how to change data into curly braces, {"status": "0" , "message": "\U9A8C\U8BC1\U7801\U9519\U8BEF", "Data": {}} This form?
------to solve the idea----------------------
It's not usually necessary, otherwise it has to be re-written.
------to solve the idea----------------------
This is what happens when you return.
echo Json_encode ($array);
You changed it.
echo Str_replace (' [] ', ' {} ', Json_encode ($array));

------to solve the idea----------------------
You can use the Json_force_object parameter

 
  

$a =array ();

Var_dump (Json_encode ($a, json_force_object));


$a =array (a);

Var_dump (Json_encode ($a, json_force_object));

$a =array ("ret" =>0);

Var_dump (Json_encode ($a, json_force_object));



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




------to solve the idea----------------------
Json_force_object (integer)
Causes a non-associative array to output a class (Object) instead of an array. This is especially useful when the array is empty and the recipient needs a Class (Object). Effective from PHP 5.3.0.
  • Related Article

    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.