date数组到星期数组和值的总和

时间:2017-03-02 00:21:04

标签: php arrays date

我正在研究将日期和数据数组转换为星期开始日期数组以及这些周内所包含日期的数据总和。

这是我拥有的和我想拥有的东西:

//array coming from some other process (mysql)    
    $data = array(
      array('product' => 'test', 'date' => '2017/02/12', 'value' => '2'), //sunday
      array('product' => 'test', 'date' => '2017/02/13', 'value' => '3'), //monday
      array('product' => 'test', 'date' => '2017/02/14', 'value' => '4'), //tuesday
      array('product' => 'test', 'date' => '2017/02/15', 'value' => '3'), //wednesday
      array('product' => 'test', 'date' => '2017/02/20', 'value' => '2'), //monday
      array('product' => 'test', 'date' => '2017/02/21', 'value' => '3'), //tuesday
      array('product' => 'test', 'date' => '2017/02/22', 'value' => '2'), //wendesday
      array('product' => 'test', 'date' => '2017/02/23', 'value' => '4'), //thursday
    );

    $starts = 'Sunday';
    //week starts on sunday  (need to be able to define this, for example this could be monday)

    //expected output array :
    $data_weekly = array(
    array('product' => 'test', 'date' => '2017/02/12', 'value' => '12'), //sunday and addition of values for that week starting on sunday
    array('product' => 'test', 'date' => '2017/02/19', 'value' => '11'), //sunday and addition of values for that week starting on sunday
    );

不确定最好的方法是什么。我是否应该开始在循环中收集年周并开始汇总值以生成第二个数组?还是有更好的更有效的方式或原生功能?

1 个答案:

答案 0 :(得分:1)

我的解决方案可以变成包裹在foreach循环中的双线程,但它不会非常易读。我已在评论中解释过,但如果您对此有疑问,请随时问我。

以下是带注释的代码:

$data = array(
    array('product' => 'test', 'date' => '2017/02/12', 'value' => '2'), //sunday
    array('product' => 'test', 'date' => '2017/02/13', 'value' => '3'), //monday
    array('product' => 'test', 'date' => '2017/02/14', 'value' => '4'), //tuesday
    array('product' => 'test', 'date' => '2017/02/15', 'value' => '3'), //wednesday
    array('product' => 'test', 'date' => '2017/02/20', 'value' => '2'), //monday
    array('product' => 'test', 'date' => '2017/02/21', 'value' => '3'), //tuesday
    array('product' => 'test', 'date' => '2017/02/22', 'value' => '2'), //wendesday
    array('product' => 'test', 'date' => '2017/02/23', 'value' => '4'), //thursday
);

// str_to_time() likes to deal with date in YYYY-MM-DD format
foreach($data as $row){
    if(date('w',strtotime(str_replace("/","-",$row["date"])))==0){
        // this is a Sunday
        $key=$row["date"];
    }else{
        // this is not a Sunday, get last Sunday
        $key=date("Y/m/d",strtotime(str_replace("/","-",$row["date"])." last Sunday")); 
    }
    if(!isset($result[$key])){
        // initialize the subarray
        $result[$key]=$row;
    }else{
        // add the new value to running value tally
        $result[$key]["value"]+=$row["value"];
    }
}

echo "<pre>";
var_export($result);
echo "</pre>";

这是输出:

array(
    '2017/02/12' => array(
        'product' => 'test',
        'date' => '2017/02/12',
        'value' => 12
     ),
     '2017/02/19' => array(
        'product' => 'test',
        'date' => '2017/02/20',
        'value' => 11
     )
)