2017-03-02 55 views
0

我正在查看將日期和數據數組轉換爲星期開始日期的數組以及那些星期中包含日期的數據總和。日期數組到週數組和值的總和

這裏是我有什麼,我想有:

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

不知道什麼是最好的方式做,這將是。我是否應該開始在一個循環中收集年份並開始總結數值以生成第二個數組?還是有更好的更有效的方式或本地功能?

+0

因此,如何在'$ data'陣列產生的? – RiggsFolly

+0

很長的故事..它來自一個MySQL數據庫經過許多循環和進程,並輸出到json,使用JS解析數據表。我需要在這個數據表上有一個「星期」切換器,所以我認爲最好的做法是在json之前修改結束數組來生成替代json,而不是在mysql中或在數據表中使用JS。 –

+1

我可以建議,在MYSQL收集階段__是你應該看的地方 – RiggsFolly

回答

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