PHP returns JSON data function instance

Source: Internet
Author: User

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

Json_encode () function usage:

?
1 echojson_encode(array(‘a‘=>‘bbbb‘,‘c‘=>‘ddddd‘);

This will generate a standard JSON-formatted data

?
123456789Ten One A - - the - - - + - + A at - - - - - in - to + - the <?php//需要执行的SQL语句//单条$sql="select id,name from tbl_user where id=1";//多条数据//$sql="select id,name from tbl_user";//调用conn.php文件进行数据库操作 require(‘Conn.php‘);//提示操作成功信息,注意:$result存在于conn.php文件中,被调用出来 if($result) {// $array=mysql_fetch_array($result,MYSQL_ASSOC);     /*数据集 $users=array(); $i=0; while($row=mysql_fetch_array($result,MYSQL_ASSOC)){ echo $row[‘id‘].‘-----------‘.$row[‘name‘].‘</br>‘; $users[$i]=$row; $i++; } echo json_encode(array(‘dataList‘=>$users)); */ /*单条数据*/ $row=mysql_fetch_row($result,MYSQL_ASSOC);   echo json_encode(array(‘jsonObj‘=>$row));}mysql_free_result($result);//释放结果mysql_close();//关闭连接?>

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:

?
1 2 3 4 5 6 7 8 9 {"UserKeyGetResponse":{"RequestName":"e99e6d63e8c712d7699f52978a","api_key_value":"41954dd9b1cb6a95802eab6810"},"error_response":{"code":"NO_ERROR","msg":"获取系统参数成功"}}可以将结果写成这样的数组形式:$respon = array(‘UserKeyGetResponse‘ => array(‘RequestName‘ => $api_request_name, ‘api_key_value‘ => $api_key_value),‘error_response‘ => array(‘code‘ => ‘NO_ERROR‘, ‘msg‘ => ‘获取系统参数成功‘));

The code is as follows:

?
1 2 3 4 5 6 7 8 9 10 11 Span style= "font-size:16px" >12 13 14 15 16 17 18 19 20 21 22 23 24 25 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‘ => ‘消息不存在‘);echo JSON($array);

The result of the operation is:

?
1 {"code":"ERROR_MSG_MISS","msg":"消息不存在"}

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.

PHP returns JSON data function instance

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.