PHP returns JSON data function instance, PHP return JSON instance _php tutorial

Source: Internet
Author: User

PHP returns JSON data function instance, PHP returns JSON instance


This article describes the use of PHP return JSON data functions, shared for everyone to reference. Here's how:

Json_encode () function usage:

echo json_encode (Array (' a ' = ' = ' bbbb ', ' c ' = ' ddddd ');

This will generate a standard JSON-formatted data

<?php//need to execute SQL statement//single $sql= "Select Id,name from Tbl_user where id=1";//Multiple data//$sql = "Select Id,name from Tbl_user";// Call conn.php file for database operation require (' conn.php ');//Prompt Operation success Information, note: $result exists in the conn.php file, is called out if ($result) {//$array =mysql_ Fetch_array ($result, MYSQL_ASSOC);   /* Data set $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 */$row =mysql_fetch_row ($result, MYSQL_ASSOC); echo json_encode (Array (' jsonobj ' = $row));} Mysql_free_result ($result);//release result mysql_close ();//close Connection?>

Above is the database to generate JSON data
Single data: {"Jsonobj": {"id": "1", "name": "LMW"}}
Multiple data: {"DataList": [{"id": "1", "name": "LMW"},{"id": "2", "Name": "XXJ"},{"id": "3", "Name": "Xxxj"}]}

Now in many cases, we need the program to return a JSON-formatted result, such as:

{"Userkeygetresponse": {"Requestname": "e99e6d63e8c712d7699f52978a", "Api_key_value": "41954dd9b1cb6a95802eab6810" }, "Error_response": {"code": "No_error", "MSG": "Get System Parameters succeeded"}} You can write the result as an array form: $respon = Array (' userkeygetresponse ' = The array (' requestname ' = = $api _request_name, ' api_key_value ' + $api _key_value), ' error_response ' = = Array (' Code ' = ' no_error ', ' msg ' = ' = ' Get system parameters successfully ');

The code is as follows:

Function arrayrecursive (& $array, $function, $apply _to_keys_also = False) {  static $recursive _counter = 0;  if (+ + $recursive _counter >) {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 not present '); Echo JSON ($array);

The result of the operation is:

{"Code": "Error_msg_miss", "MSG": "Message does not exist"}

The client can parse the result, and of course the error code should be replaced with a number.
This is much better. We are showing the direct Chinese, of course, the 16 encoding is not a problem.

I hope this article is helpful to everyone's PHP programming.


How PHP handles the JSON data returned from the back table

You are the AJAX request sent with jquery! You can use $.each (msg,function (item,index) {
Item represents each Object!
Index stands for Indexes
});

PHP implementation http crawl want to implement the Fetchurl function in the local SAE can, return JSON format data, guide

Refer to the principle of the SAE Distributed crawl HTTP page

Reference Address: E.sae.sina.com.cn/...pstore

http://www.bkjia.com/PHPjc/891106.html www.bkjia.com true http://www.bkjia.com/PHPjc/891106.html techarticle PHP returns JSON data function instance, PHP return JSON instance This article describes the use of PHP return JSON data functions, share to everyone for your reference. The specific method is as follows: Json_encode () Letter ...

  • 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.