我已經從一個月的表數據編碼json數組,如下所示,但如何組合所有數據後,如何將多個數據表與其他月份在一個json數組和GROUP BY部分組合?如何合併多個表數據並將json編碼爲一個數組?
<?php
include("dbconfig.php");
$sql = "SELECT dept, SUM(ttlot) AS sum_ot, SUM(ttlnorm) AS sum_norm
FROM month_jan
GROUP BY dept
ORDER BY sum_ot DESC";
$result = mysqli_query($dbconfig, $sql) or die("Error in Selecting " . mysqli_error($dbconfig));
$category = array();
$category['name'] = 'Dept';
$series1 = array();
$series1['name'] = 'Normal';
$series2 = array();
$series2['name'] = 'OT';
$emparray = array();
while ($row = mysqli_fetch_assoc($result)) {
$category['data'][] = $row['dept'];
$series1['data'][] = $row['sum_norm'];
$series2['data'][] = $row['sum_ot'];
}
$result = array();
array_push($result,$category);
array_push($result,$series1);
array_push($result,$series2);
$json = json_encode($result,JSON_NUMERIC_CHECK);
echo $json;
mysqli_close($dbconfig);
?>
產出月月:
[{"name":"Dept","data":["CNC","MACH","ANOD","BUFF","CAST","POLISH","SL","EPT","TUMB","TOOL","SHOT","QC","LOG","MAIN","LC","WWT","OG","NPD","E-COAT","SFT"]},{"name":"Normal","data":[47429.1,39975.7,34553.8,49075.9,28316.3,21237.1,13492.5,5848.2,7691.1,6963.9,5636.1,7555.8,5821.9,2161.2,1812,1191.7,1479.1,1299.6,11542.6,602]},{"name":"OT","data":[20041,17874,14431,13535.5,8800.5,5613.5,3569.5,3101,2327,2278,2237,2142,1810,942,690,456,297,110.5,66,50.5]}]
什麼結果,我結合四個月後,想:
[{"name":"Month","data":["Jan","Feb","Mac","Apr"]},{"name":"Normal","data":[504291,409757,295538,430759]},{"name":"OT","data":[89041,96874,81431,80535]}]
有誰可以幫我解決這個問題?