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.