格式化 JSON_ENCODED 数据 PHP Highcharts

Posted

技术标签:

【中文标题】格式化 JSON_ENCODED 数据 PHP Highcharts【英文标题】:Formatting JSON_ENCODED data PHP Highcharts 【发布时间】:2018-05-09 18:34:57 【问题描述】:

我正在尝试从数据库中获取数据并用它填充 Highstock 图表,之前我将图表的“数据:”选项分配给从搜索生成的数据表,我意识到数据会绘制系列,但顺序随机。这是我尝试过的:

      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;
          

代码的结果有相同数据的多次迭代: ["[1483833600000, 2]"]["[1483833600000, 2]","[1484697600000, 1]"]["[1483833600000, 2]","[1484697600000, 1]","[149117400000, 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]"]0000000, 30]"000004833 , 2]","[1484697600000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]","[1503010800000, 3 ]","[1503183600000, 30]","[1503270000000, 12]"]["[1483833600000, 2]","[14846976 00000, 1]","[1491174000000, 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]","[1503010800000, 3]","[15031836000000 30]","[1503270000000, 12]","[1503356400000, 10]"]["[1483833600000, 2]","[1484697600000, 1]","[1491174000000, 1]","[1492222000000, 2 ]","[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]","[1502319600020, 11]"0001004924 , 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]","[15024924000000, 3]","00803 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]","[1497222000 000, 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]","01010 , 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]","[14846976009100, 1]"[1] , 1]","[1497222000000, 22]","[1502319600000, 11]","[1502492400000, 3]","[1503010800000, 3]","[1503183600000, 30]","[1502270000000, 1 ]","[1503356400000, 10]","[1503961200000, 9]","[1506985200000, 3]","[1507158000000, 3]","[1508367600000, 31]","[1509408000000, 3]" ,"[1509667200000, 8]","[1510790400000, 10]","[1511049600000, 12]","[1511395200000, 12]","[1514764800000, 2]"]

数据应如下所示: Format

参考文件: High Charts

【问题讨论】:

【参考方案1】:

记住json_encode必须使用一次

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;

【讨论】:

嗯,这对我来说是一个愚蠢的错误...... lool 感觉就像一个超级菜鸟!谢谢,已经做了一个通宵思考出了什么问题!

以上是关于格式化 JSON_ENCODED 数据 PHP Highcharts的主要内容,如果未能解决你的问题,请参考以下文章

回显所有 json_encoded 行

当从 vuejs 组件中的 props 解码回来时,Vuejs 改变了 json_encoded 数组的顺序

将数据从 PHP 脚本发送到 iOS

php 时间格式化

php时间问题?mysql数据库的时间格式(Y-M-D H:I:S) 在PHP页面想这样显示(Y-M-D) ('.$rows['ndate'].')

php时间格式转换