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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失