我想用Google Column Chart將我的數據可視化。但是我無法從MySql獲得適當的數據結構。將MySql查詢結果轉換爲正確的JSON數據
我目前的查詢結果:
array(6){
[0]=> array(3){["id"]=>"1" ["count"]=>"6" ["day"]=>"15-01-01"}
[1]=> array(3){["id"]=>"2" ["count"]=>"3" ["day"]=>"15-01-01" }
[2]=> array(3){["id"]=>"3" ["count"]=>"1" ["day"]=>"15-01-01" }
[3]=> array(3){["id"]=>"1" ["count"]=>"6" ["day"]=>"15-01-02" }
[4]=> array(3){["id"]=>"2" ["count"]=>"4" ["day"]=>"15-01-02" }
[5]=> array(3){["id"]=>"3" ["count"]=>"6" ["day"]=>"15-01-02" }
}
雖然谷歌圖表需要以下格式:
var data = google.visualization.arrayToDataTable([
['Date', '15-01-01', '15-01-02', { role: 'annotation' } ],
['1', 6, 6,],
['2', 3, 4,],
['3', 1, 6,],
]);
如何將我的數據轉換成合適的格式?
UPDATE
我嘗試這樣做:
$dateArray= array();
$output= "var data = google.visualization.arrayToDataTable([";
$output .= "['Date',";
foreach($mySqlResult as $val) {
if (in_array($val['day'], $dateArray)) {
continue;
}
$dateArray[] = $val['day'];
$output .= "'".$val['day']."',";
}
$output .= " { role: 'annotation' } ],";
foreach($mySqlResult as $val) {
$output .= "['".$val['id']."',".$val['count']."],";
}
$output .= "]);";
這給了我下面的結果:
"var data = google.visualization.arrayToDataTable(
[['Date','15-01-01','15-01-02', { role: 'annotation' } ],
['1',6,],['2',3,],['3',1,],['1',6,],['2',4,],['3',6,],]);"
需要還沒什麼。如何創建每個ID與所有計數行:
['1', 6, 6,],['2', 3, 4,],['3', 1, 6,],
你已經嘗試什麼到目前爲止 –
你能證明你的代碼,什麼是 –
@SharmaVikram我添加鏈接到谷歌柱形圖 – julie