美文网首页
处理杂乱的数据。

处理杂乱的数据。

作者: RossWen | 来源:发表于2017-05-11 11:05 被阅读0次

    已知数据

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

    相关文章

      网友评论

          本文标题:处理杂乱的数据。

          本文链接:https://www.haomeiwen.com/subject/rvdsattx.html