dongzhong3688 2017-11-26 11:03 采纳率: 0%
浏览 72
已采纳

格式化JSON_ENCODED数据PHP Highcharts

I am trying to get data from a database and populate the Highstock charts with it, previously I was assigning the 'data: ' option of the chart to the datatable that was generated from the search, I realised the data would plot the series but in a random order. Here is what I have tried:

      function json_original()
{

$sql = "SELECT UNIX_TIMESTAMP(t1.`date`) as `date`, sum(t1.`present`) as `present` from record_attendance_cell t1 group by t1.`date`";

 $present_g = $this->db->query($sql)->result_array();

   foreach($present_g as $row) {
       $value = $row['present'];
       $datetime = $row['date'] * 1000; // convert from Unix timestamp to JavaScript time
       $data[] = "[$datetime, $value]";
       // echo join($data, ',');
       $result = json_encode($data);
       echo $result;
        }  
}

The result of the code has many iterations of the same data: ["[1483833600000, 2]"]["[1483833600000, 2]","[1484697600000, 1]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]","[1503010800000, 3]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]","[1503010800000, 3]","[1503183600000, 30]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]","[1503010800000, 3]","[1503183600000, 30]","[1503270000000, 12]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]","[1503010800000, 3]","[1503183600000, 30]","[1503270000000, 12]","[1503356400000, 10]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]","[1503010800000, 3]","[1503183600000, 30]","[1503270000000, 12]","[1503356400000, 10]","[1503961200000, 9]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]","[1503010800000, 3]","[1503183600000, 30]","[1503270000000, 12]","[1503356400000, 10]","[1503961200000, 9]","[1506985200000, 3]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]","[1503010800000, 3]","[1503183600000, 30]","[1503270000000, 12]","[1503356400000, 10]","[1503961200000, 9]","[1506985200000, 3]","[1507158000000, 3]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]","[1503010800000, 3]","[1503183600000, 30]","[1503270000000, 12]","[1503356400000, 10]","[1503961200000, 9]","[1506985200000, 3]","[1507158000000, 3]","[1508367600000, 31]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]","[1503010800000, 3]","[1503183600000, 30]","[1503270000000, 12]","[1503356400000, 10]","[1503961200000, 9]","[1506985200000, 3]","[1507158000000, 3]","[1508367600000, 31]","[1509408000000, 3]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]","[1503010800000, 3]","[1503183600000, 30]","[1503270000000, 12]","[1503356400000, 10]","[1503961200000, 9]","[1506985200000, 3]","[1507158000000, 3]","[1508367600000, 31]","[1509408000000, 3]","[1509667200000, 8]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]","[1503010800000, 3]","[1503183600000, 30]","[1503270000000, 12]","[1503356400000, 10]","[1503961200000, 9]","[1506985200000, 3]","[1507158000000, 3]","[1508367600000, 31]","[1509408000000, 3]","[1509667200000, 8]","[1510790400000, 10]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]","[1503010800000, 3]","[1503183600000, 30]","[1503270000000, 12]","[1503356400000, 10]","[1503961200000, 9]","[1506985200000, 3]","[1507158000000, 3]","[1508367600000, 31]","[1509408000000, 3]","[1509667200000, 8]","[1510790400000, 10]","[1511049600000, 12]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]","[1503010800000, 3]","[1503183600000, 30]","[1503270000000, 12]","[1503356400000, 10]","[1503961200000, 9]","[1506985200000, 3]","[1507158000000, 3]","[1508367600000, 31]","[1509408000000, 3]","[1509667200000, 8]","[1510790400000, 10]","[1511049600000, 12]","[1511395200000, 12]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]","[1503010800000, 3]","[1503183600000, 30]","[1503270000000, 12]","[1503356400000, 10]","[1503961200000, 9]","[1506985200000, 3]","[1507158000000, 3]","[1508367600000, 31]","[1509408000000, 3]","[1509667200000, 8]","[1510790400000, 10]","[1511049600000, 12]","[1511395200000, 12]","[1514764800000, 2]"]

The data should look like this: Format

Reference document: High Charts

  • 写回答

1条回答 默认 最新

  • dtxzwdl08169 2017-11-26 11:10
    关注

    Remember that json_encode must be used once:

    foreach($present_g as $row) {
        $value = $row['present'];
        $datetime = $row['date'] * 1000; // convert from Unix timestamp to JavaScript time
        // Also remove quotes here, you don't need a string
        $data[] = [$datetime, $value];
    }  
    $result = json_encode($data);
    echo $result;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog