美文网首页
工作遇到的PHP数组数据合并、排序问题

工作遇到的PHP数组数据合并、排序问题

作者: 铁匠简记 | 来源:发表于2018-04-27 16:51 被阅读6次

    机械的增删改查做多了,脑子都转不动了,

    今天遇到一个需要运用算法的问题,愣是研究了半天,特此记录:

    下面开始我的表演:

    根据一下数组中create_time字段求每天各个字段平均值

    数组数据源如下:

    $data=array(43) {
      [0] => array(8) {
        ["id"] => int(6)
        ["PM1"] => float(1)
        ["PM25"] => float(2)
        ["PM10"] => float(3)
        ["humidity"] => float(4)
        ["temperature"] => float(5)
        ["lid"] => int(1)
        ["create_time"] => string(10) "2018-01-29"
      }
      [1] => array(8) {
        ["id"] => int(7)
        ["PM1"] => float(1.1)
        ["PM25"] => float(2)
        ["PM10"] => float(3.3)
        ["humidity"] => float(4.5)
        ["temperature"] => float(5.8)
        ["lid"] => int(1)
        ["create_time"] => string(10) "2018-01-29"
      }
      [2] => array(8) {
        ["id"] => int(8)
        ["PM1"] => float(11)
        ["PM25"] => float(22)
        ["PM10"] => float(33)
        ["humidity"] => float(44)
        ["temperature"] => float(55)
        ["lid"] => int(1)
        ["create_time"] => string(10) "2018-02-01"
      }
      [3] => array(8) {
        ["id"] => int(9)
        ["PM1"] => float(11)
        ["PM25"] => float(22)
        ["PM10"] => float(33)
        ["humidity"] => float(44)
        ["temperature"] => float(55)
        ["lid"] => int(1)
        ["create_time"] => string(10) "2018-02-01"
      }
      [4] => array(8) {
        ["id"] => int(10)
        ["PM1"] => float(17)
        ["PM25"] => float(21)
        ["PM10"] => float(23)
        ["humidity"] => float(114)
        ["temperature"] => float(252)
        ["lid"] => int(1)
        ["create_time"] => string(10) "2018-02-02"
      }
      [5] => array(8) {
        ["id"] => int(11)
        ["PM1"] => float(17)
        ["PM25"] => float(21)
        ["PM10"] => float(23)
        ["humidity"] => float(114)
        ["temperature"] => float(252)
        ["lid"] => int(1)
        ["create_time"] => string(10) "2018-02-03"
      }
      
      [6] => array(8) {
        ["id"] => int(23)
        ["PM1"] => float(15)
        ["PM25"] => float(19)
        ["PM10"] => float(21)
        ["humidity"] => float(129)
        ["temperature"] => float(252)
        ["lid"] => int(1)
        ["create_time"] => string(10) "2018-02-03"
      }
      [7] => array(8) {
        ["id"] => int(24)
        ["PM1"] => float(15)
        ["PM25"] => float(19)
        ["PM10"] => float(21)
        ["humidity"] => float(124)
        ["temperature"] => float(252)
        ["lid"] => int(1)
        ["create_time"] => string(10) "2018-02-03"
      }
      [8] => array(8) {
        ["id"] => int(25)
        ["PM1"] => float(15)
        ["PM25"] => float(19)
        ["PM10"] => float(21)
        ["humidity"] => float(121)
        ["temperature"] => float(252)
        ["lid"] => int(1)
        ["create_time"] => string(10) "2018-02-04"
      }
      [9] => array(8) {
        ["id"] => int(26)
        ["PM1"] => float(15)
        ["PM25"] => float(19)
        ["PM10"] => float(21)
        ["humidity"] => float(120)
        ["temperature"] => float(252)
        ["lid"] => int(1)
        ["create_time"] => string(10) "2018-02-04"
      }
      [10] => array(8) {
        ["id"] => int(27)
        ["PM1"] => float(15)
        ["PM25"] => float(19)
        ["PM10"] => float(21)
        ["humidity"] => float(119)
        ["temperature"] => float(253)
        ["lid"] => int(1)
        ["create_time"] => string(10) "2018-02-04"
      }
    }
    

    首先把数组按create_time分组

    public function group()
    {
      $flag = $data[0]['create_time'];
      $new[$flag][] = $data[0];
      $len = count($data);
      for ($i=1;$i< $len; $i++){
          if ( $data[$i]['create_time'] == $flag ){
              $new[$flag][]= $data[$i];
          }else{
              $flag = $data[$i]['create_time'];
              $new[$flag][] = $data[$i];
          }
      }
      $data = self::getAvgData($new);
    }
    

    下一步,对每天的数据求平均值:

    protected static function getAvgData($new)
    {
        if (!is_array($new)){
            exit();
        }
        foreach ($new as $key => $value){
            $flag = $value[0];
            for ( $i = 1; $i < count($value); $i++ ){
                $flag['PM1'] = $flag['PM1']  + $value[$i]['PM1'];
                $flag['PM25'] = $flag['PM25'] + $value[$i]['PM25'];
                $flag['PM10'] = $flag['PM10'] + $value[$i]['PM10'];
                $flag['temperature'] =$flag['temperature'] + $value[$i]['temperature'];
                $flag['lid'] =$flag['lid'] + $value[$i]['lid'];
            }
            array_shift($flag);
            foreach ($flag as $k => $v){
                $flag[$k] = $v / count($value);
            }
            $rest[$key] = $flag;
        }
        //return $rest;
        print_r($rest);
    }
    

    大功告成:

    array(3) {
      ["2018-01-29"] => array(7) {
        ["PM1"] => float(1.05)
        ["PM25"] => float(2)
        ["PM10"] => float(3.15)
        ["humidity"] => float(2)
        ["temperature"] => float(5.4)
        ["lid"] => int(1)
        ["create_time"] => int(1009)
      }
      ["2018-02-02"] => array(7) {
        ["PM1"] => float(15.466666666667)
        ["PM25"] => float(20.4)
        ["PM10"] => float(23.6)
        ["humidity"] => float(2.9333333333333)
        ["temperature"] => float(225.6)
        ["lid"] => int(1)
        ["create_time"] => float(134.53333333333)
      }
      ["2018-02-03"] => array(7) {
        ["PM1"] => float(15)
        ["PM25"] => float(19)
        ["PM10"] => float(21)
        ["humidity"] => float(4.9615384615385)
        ["temperature"] => float(252.53846153846)
        ["lid"] => int(1)
        ["create_time"] => float(77.615384615385)
      }
    }
    

    相关文章

      网友评论

          本文标题:工作遇到的PHP数组数据合并、排序问题

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