已知數(shù)據(jù)
最后得到的每個(gè)時(shí)間點(diǎn)是唯一的。但是可能會有多個(gè)不同的score=TotalCount數(shù)據(jù)蔓姚,未給score=TotalCount則代表score=0捕虽,score總類型有五種,最后需要得到的數(shù)據(jù)類型為
{
time:array(),
score1:array(),
score1:array(),
score1:array(),
score1:array()
}
我在第一次做的邏輯為:
1新建一個(gè)時(shí)間數(shù)組$time = array();
2循環(huán)每一行數(shù)據(jù)坡脐,首先判斷時(shí)間數(shù)組里有沒有行數(shù)據(jù)中的時(shí)間key值泄私,如果沒有添加時(shí)間,再次判斷score類型,有score晌端,添加對應(yīng)Count捅暴,沒有添加0。
if(判斷時(shí)間數(shù)組里有沒有行數(shù)據(jù)中的時(shí)間key值)
{
if(判斷score類型)
{
right添加score對應(yīng)的TotalCount值斩松。
wrong添加score=0
}
}else{
if(判斷score類型)
{
修改之前重復(fù)時(shí)間點(diǎn)的score對應(yīng)的值伶唯,
}
}
ok,寫了這多多惧盹,最后得到數(shù)據(jù)結(jié)果的時(shí)候出了問題乳幸,而且由于代碼太多冗雜,讓人非常頭大钧椰,因此改變思路在處理數(shù)據(jù)的時(shí)候使用對象/多維數(shù)組去處理粹断,
$timeArr = array();
$resArr0 = array("12"=>0,"11"=>0,"10"=>0,"09"=>0,"08"=>0,"07"=>0,"06"=>0,"05"=>0,"04"=>0,"03"=>0,"02"=>0,"01"=>0,"00"=>0,"23"=>0,"22"=>0,"21"=>0,"20"=>0,"19"=>0,"18"=>0,"17"=>0,"16"=>0,"15"=>0,"14"=>0,"13"=>0,);
$resArr20 = array("12"=>0,"11"=>0,"10"=>0,"09"=>0,"08"=>0,"07"=>0,"06"=>0,"05"=>0,"04"=>0,"03"=>0,"02"=>0,"01"=>0,"00"=>0,"23"=>0,"22"=>0,"21"=>0,"20"=>0,"19"=>0,"18"=>0,"17"=>0,"16"=>0,"15"=>0,"14"=>0,"13"=>0,);
$resArr40 = array("12"=>0,"11"=>0,"10"=>0,"09"=>0,"08"=>0,"07"=>0,"06"=>0,"05"=>0,"04"=>0,"03"=>0,"02"=>0,"01"=>0,"00"=>0,"23"=>0,"22"=>0,"21"=>0,"20"=>0,"19"=>0,"18"=>0,"17"=>0,"16"=>0,"15"=>0,"14"=>0,"13"=>0,);
$resArr60 = array("12"=>0,"11"=>0,"10"=>0,"09"=>0,"08"=>0,"07"=>0,"06"=>0,"05"=>0,"04"=>0,"03"=>0,"02"=>0,"01"=>0,"00"=>0,"23"=>0,"22"=>0,"21"=>0,"20"=>0,"19"=>0,"18"=>0,"17"=>0,"16"=>0,"15"=>0,"14"=>0,"13"=>0,);
$resArr80 = array("12"=>0,"11"=>0,"10"=>0,"09"=>0,"08"=>0,"07"=>0,"06"=>0,"05"=>0,"04"=>0,"03"=>0,"02"=>0,"01"=>0,"00"=>0,"23"=>0,"22"=>0,"21"=>0,"20"=>0,"19"=>0,"18"=>0,"17"=>0,"16"=>0,"15"=>0,"14"=>0,"13"=>0,);
while($row = mysqli_fetch_assoc($result)){
array_push($taskUID, $row['TaskUID'].'&M='.$row['TaskCommandID']);
if(!in_array(substr($row['TaskExecutionTime'],stripos($row['TaskExecutionTime'],' ')+1,5),$timeArr)){
array_push($timeArr,substr($row['TaskExecutionTime'],stripos($row['TaskExecutionTime'],' ')+1,5));
}
$timeIndex = substr($row['TaskExecutionTime'],stripos($row['TaskExecutionTime'],' ')+1,2);
if($row['score']>=0&&$row['score']<10020){
$resArr0[$timeIndex] += $row['TotalCount'];
}
if($row['score']>=10020&&$row['score']<10040){
$resArr20[$timeIndex] += $row['TotalCount'];
}
if($row['score']>=10040&&$row['score']<10060){
$resArr40[$timeIndex] += $row['TotalCount'];
}
if($row['score']>=10060&&$row['score']<10080){
$resArr60[$timeIndex] += $row['TotalCount'];
}
if($row['score']>=10080){
$resArr80[$timeIndex] += $row['TotalCount'];
}
}
$finalArray = array('timeSeries'=>$timeArr,'score0'=>$score0,'score20'=>$score20,'score40'=>$score40,'score60'=>$score60,'score80'=>$score80,'taskUIDs'=>$taskUID);