This article mainly introduces php return json data function instances and describes json_encode () function usage in detail. it is of great practical value in PHP database programming, for more information about how to use the json data function returned by php, see the following example. The specific method is as follows:
Json_encode () function usage:
echo json_encode(array('a'=>'bbbb','c'=>'ddddd');
In this way, a standard json format data is generated.
<? Php // SQL statement to be executed // Single $ SQL = "select id, name from tbl_user where id = 1 "; // multiple data records // $ SQL = "select id, name from tbl_user"; // call conn. php file for database operation require ('Conn. php '); // The message indicating successful operation is displayed. note: $ result exists in conn. in the php file, if ($ result) {// $ array = mysql_fetch_array ($ result, MYSQL_ASSOC);/* dataset $ users = array (); $ I = 0; while ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC) {echo $ row ['id']. '-----------'. $ row ['name'].'
'; $ Users [$ I] = $ row; $ I ++;} echo json_encode (array ('datalist' => $ users )); * // * single data entry */$ row = mysql_fetch_row ($ result, MYSQL_ASSOC); echo json_encode (array ('jsonobj '=> $ row ));} mysql_free_result ($ result); // release result mysql_close (); // close the connection?>
Above is the json data generated by the database
Single data entry: {"jsonObj": {"id": "1", "name": "lmw "}}
Multiple data entries: {"dataList": [{"id": "1", "name": "lmw" },{ "id": "2 ", "name": "xxj" },{ "id": "3", "name": "xxxj"}]}
In many cases, the program needs to return a result in Json format, for example:
{"UserKeyGetResponse": {"RequestName": "Response", "api_key_value": "response"}, "error_response": {"code": "NO_ERROR", "msg ": "system parameter retrieved"} you can write the result as an array: $ respon = array ('userkeygetresponse' => array ('requestname' => $ api_request_name, 'api _ key_value '=> $ api_key_value), 'error _ response' => array ('code' => 'No _ error ', 'MSG '=> 'system parameter Retrieved successfully '));
The code is as follows:
Function arrayRecursive (& $ array, $ function, $ apply_to_keys_also = false) {static $ recursive_counter = 0; if (++ $ recursive_counter> 1000) {die ('possible deep recursion attack');} foreach ($ array as $ key => $ value) {if (is_array ($ value )) {arrayRecursive ($ array [$ key], $ function, $ apply_to_keys_also);} else {$ array [$ key] = $ function ($ value );} if ($ apply_to_keys_also & is_string ($ key) {$ new_k Ey = $ function ($ key); if ($ new_key! = $ Key) {$ array [$ new_key] = $ array [$ key]; unset ($ array [$ key]) ;}}$ recursive_counter --;} g: $ error_respon = array ('code' => 'Error _ MSG_MISS ', 'MSG' => 'message nonexistent '); echo JSON ($ array );
The running result is:
{"Code": "ERROR_MSG_MISS", "msg": "The message does not exist "}
The client can parse this result. of course, the error code should be replaced by a number.
This is much better. We now display Chinese characters directly. of course, it is okay to display the hexadecimal encoding.
I hope this article will help you with PHP programming.