处理杂乱的数据。

已知数据

dbData.png

最后得到的每个时间点是唯一的。但是可能会有多个不同的score=TotalCount数据,未给score=TotalCount则代表score=0,score总类型有五种,最后需要得到的数据类型为

{
time:array(),
score1:array(),
score1:array(),
score1:array(),
score1:array()
}

我在第一次做的逻辑为:
1新建一个时间数组$time = array();
2循环每一行数据,首先判断时间数组里有没有行数据中的时间key值,如果没有添加时间,再次判断score类型,有score,添加对应Count,没有添加0。
if(判断时间数组里有没有行数据中的时间key值)
{
if(判断score类型)
{
right添加score对应的TotalCount值。
wrong添加score=0
}
}else{
if(判断score类型)
{
修改之前重复时间点的score对应的值,
}
}
ok,写了这多多,最后得到数据结果的时候出了问题,而且由于代码太多冗杂,让人非常头大,因此改变思路在处理数据的时候使用对象/多维数组去处理,

$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);

推荐阅读更多精彩内容

  • 百战程序员_ Java1573题 QQ群:561832648489034603 掌握80%年薪20万掌握50%年薪...
    Albert陈凯阅读 13,424评论 3 33
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 5,747评论 4 12
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 26,943评论 17 394
  • 刚刚重温哈利波特时,想起来小时候和弟弟一起在一人一个小板凳坐在电脑前看哈利波特之死亡圣器那一部,旁边的老爸在睡午觉...
    柚子溪阅读 25评论 0 0
  • 一个星期过的如此之快,虽然每天大部分时间在图书馆里度过,但却不全然在学习中。 看着考研的时间从270到150...
    蓝曦LL阅读 24评论 0 0