Home  >  Article  >  Backend Development  >  Introduction to usage examples of json_encode in php

Introduction to usage examples of json_encode in php

黄舟
黄舟Original
2017-11-15 15:36:512680browse

In the last article, we introduced you to the detailed explanation of the json_encode() function in php, and we know that it can only be used in versions above php5.2. So today we will briefly introduce json_encode to us. The method of use is very simple and practical, and I recommend it to everyone.

1. Query the data from the database and put it in the array

$query=mysql_query($SQL);
 while($row = mysql_fetch_array($query)){         
 $xdata[]=$row['EventDate'];         
 $ydata[]=intval($row['data']);    
 }

2. Convert the data into json

$data_arr=array($xdata,$ydata)
json_encode($data_arr);

3. AJAX call data in HTML page

$.ajax({
type: "Get",   
   url: "columndata.php?r=" + Math.floor(Math.random() * 1000 + 1), 
   data: { 'BeginTime': "" + beginTime + "", "EndTime": "" + endTime + "" , "keyword": "" + keyword + "" },
   dataType: "text",   
   global: false,  
   async: false,   
   success: function (strReult) {
    if (strReult == "-1") { alert("fail!"); return; }
    var jsondata = eval("(" + strReult + ")");
       var xData = jsondata[0];                                
     var yData = jsondata[1];
       var namestr = jsondata[2];
   }, 
   error: function () {
    alert("fail!");
   }
  });

Summary:

Believe it through studying this article Everyone has a better understanding of how to use json_encode in php. I hope it will be helpful to your work!

Related recommendations:

Detailed explanation of json_encode() function in php


PHP json_encode() function introduction


##PHP json_encode() function detailed explanation and Chinese garbled problem

The above is the detailed content of Introduction to usage examples of json_encode in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn