Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 日期数组到周数组和值之和_Php_Arrays_Date - Fatal编程技术网

Php 日期数组到周数组和值之和

Php 日期数组到周数组和值之和,php,arrays,date,Php,Arrays,Date,我正在研究如何将一个日期和数据数组转换为一个星期开始日期数组,以及这些星期中包含的天数的数据总和 以下是我所拥有的和我想要的: //array coming from some other process (mysql) $data = array( array('product' => 'test', 'date' => '2017/02/12', 'value' => '2'), //sunday array('product' =&

我正在研究如何将一个日期和数据数组转换为一个星期开始日期数组,以及这些星期中包含的天数的数据总和

以下是我所拥有的和我想要的:

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

不知道最好的方法是什么。我应该开始在循环中收集yearweek并开始对值求和以生成第二个数组吗?或者有更好的方法或本机函数吗?

我的解决方案可以转换为一个两行的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>";

那么,
$data
数组是如何创建的呢?说来话长。。它来自一个mysql数据库,经过多次循环和处理,并输出到json,json使用JS解析数据表。我需要在这个数据表上有一个“星期”开关,因此,我认为最好是在json之前修改end array以生成另一个json,而不是在mysql中或在datatable中使用JS。我建议回到mysql收集阶段,您应该看看这个页面有30个表,因此,我需要创建/修改并连接30个不同的mysql查询,因为这些查询的值是各种不同的度量(我只是简化了示例),另外还有5个页面,表更少,但输出格式相同,所以,如果我有一个单一的功能来修改,如上所述会更好,我想这样我就不会迷失在我需要修改的时候
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
     )
)