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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 冻品电商平台的搜索是怎么实现的
  • ¥15 如何搞一个可以控制、显示马达频率
  • ¥15 WPF动态创建页面内容
  • ¥15 如何对TBSS的结果进行统计学的分析已完成置换检验,如何在最终的TBSS输出结果提取除具体值及如何做进一步相关性分析
  • ¥15 SQL数据库操作问题
  • ¥100 关于lm339比较电路出现的问题
  • ¥15 Matlab安装yalmip和cplex功能安装失败
  • ¥15 加装宝马安卓中控改变开机画面
  • ¥15 STK安装问题问问大家,这种情况应该怎么办
  • ¥15 关于罗技鼠标宏lua文件的问题