Php returns the json data function example. php returns the json function _ PHP Tutorial

Source: Internet
Author: User
Tags php json
Php returns the json data function example, and php returns the json function. Php returns the json data function example. php returns the json function json_encode. Echojson_encode (array (abbbb, cddddd); this will generate a standard json format data. php returns the json data function example. php returns the json function.

Json_encode () function usage.

Echo json_encode (array ('a' => 'bbbbb', 'C' => 'ddddd ');

In this way, a standard json format data is generated.

The code is as follows:

// SQL statement to be executed
// Single
$ SQL = "select id, name from tbl_user where id = 1 ";
// Multiple data entries
// $ SQL = "select id, name from tbl_user ";

// Call the conn. php file for database operations
Require ('Conn. php ');

// Message indicating successful operation. note: $ result exists in the conn. php file and is called.
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

?>

The 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:

The code is as follows:
{
"UserKeyGetResponse ":
{"RequestName": "e99e6d63e8c712d7699f52978a", "api_key_value": "41954dd9b1cb6a95802eab6810 "},
"Error_response ":
{"Code": "NO_ERROR (www.111cn.net)", "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 '));

Code

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_key = $ 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 );

Result:
{"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 can display Chinese characters directly. of course, it is okay to display the hexadecimal encoding.
From: http://www.111cn.net/phper/php/62544.htm


How does PHP process the json data returned by the backend?

You are sending an ajax request with jquery! You can use $. each (msg, function (item, index ){
// Item indicates each object!
// Index indicates the index
});

Example of how to use json code in PHP

Use the json_decode function to decrypt data .. The JSON function used by PHP is: json_encode ($ PHPcode );
The JSON parsing function of PHP is: json_decode ($ JSONcode); therefore, there are many JSON forms, and different forms are different after PHP interprets them. // Form 1: it is completely in the form of an object. this form of data is also called a related array in Javascript. What is different from a general array is that, it can be accessed through string indexing (using "[]" or ". $ json = '{"item1": {"item11": {"n": "chenling", "m": "llll "}, "sex": "male", "age": "25"}, "item2": {"item21": "ling", "sex": "female ", "age": "24"} '; $ J = json_decode ($ json); print_r ($ J ); let's take a look at this article on php json data operations from the php beginner's Portal: www.phpnewer.com/index.php/Cjwt/detail/id/147.

Json_encode () function usage. Echo json_encode (array ('a' = 'bbbbbb', 'C' = 'ddddd '); then a standard json format data is generated...

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.