how to use json_encode

前端 未结 2 1320
轮回少年
轮回少年 2020-12-03 02:33

I\'m dealing with highcharts with dynamic data (values retrieved from database). By writing a query i was able to retrieve the following data from the table

         


        
相关标签:
2条回答
  • 2020-12-03 02:44

    json_encode is a convenience method to convert an array into JSON format. To have the output you provided, you will need an array of arrays. Each sub-array has keys "name" and "data", where "name" is the Item column, and "data" is another array containing values from 2011 and 2012.

    $results = mysql_query("...");
    $arr = array();
    
    while ($row = mysql_fetch_assoc($results))
    {
        $name = $row['Item'];
        $data = array($row['2011'], $row['2012']);
    
        $arr[] = array('name' => $name, 'data' => $data);
    }
    
    echo json_encode($arr);
    
    0 讨论(0)
  • 2020-12-03 02:49
    1. Loop through the database results and put the results in an array
    2. JSON encode the array
    0 讨论(0)
提交回复
热议问题