我试图从数据库中获取数据并用它填充Highstock图表,之前我曾将图表的“ data:”选项分配给通过搜索生成的数据表,但我意识到数据可以绘制序列,但是在随机顺序。这是我尝试过的:
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]”,“ [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]“,” [14846976 00000,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]“,” [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]“,” [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]”]
数据应如下所示:
Format
参考文件:
High Charts
最佳答案
请记住,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;
关于php - 格式化JSON_ENCODED数据PHP Highcharts,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47495390/