簡體   English   中英

根據子數組中的日期拆分多維數組

[英]Split multidimensional arrays according to date in child array

我有以下多維日期數組

array(
    0  => array("TransDate" => "2019-10-28 08:31:02", "Amount" => "12.00"),
    1  => array("TransDate" => "2019-10-28 09:09:14", "Amount" => "12.00"),
    2  => array("TransDate" => "2019-10-28 09:17:14", "Amount" => "12.00"),
    3  => array("TransDate" => "2019-10-28 09:30:14", "Amount" => "12.00"),
    4  => array("TransDate" => "2019-10-28 09:35:14", "Amount" => "12.00"),
    5  => array("TransDate" => "2019-10-28 10:50:14", "Amount" => "12.00"),
    6  => array("TransDate" => "2019-10-28 10:58:14", "Amount" => "12.00"),
    7  => array("TransDate" => "2019-10-28 10:58:14", "Amount" => "12.00"),
    8  => array("TransDate" => "2019-10-28 11:40:14", "Amount" => "12.00"),
    9  => array("TransDate" => "2019-10-28 11:49:14", "Amount" => "12.00"),
    10 => array("TransDate" => "2019-10-28 23:50:14", "Amount" => "12.00")
);

要求

需要將上述數組拆分為多個塊,以便每個塊都具有在 60 分鍾內具有TransDate元素。

以下塊將更清楚地說明實際需要的結果。

需要的結果

第一塊

//index 1 through index 3, the TransDate, they all are under 60 minutes difference from index 0
    array(
        0  => array("TransDate" => "2019-10-28 08:31:02", "Amount" => "12.00"),
        1  => array("TransDate" => "2019-10-28 09:09:14", "Amount" => "12.00"),
        2  => array("TransDate" => "2019-10-28 09:17:14", "Amount" => "12.00"),
        3  => array("TransDate" => "2019-10-28 09:30:14", "Amount" => "12.00")
    );

第二塊

//the date is more than 60 minutes from the last element of the previous chunk. 
//As we don't have anything that falls within 60 minutes from 
//2019-10-28 09:35:14 (first element below), this chunk will have only one element
array(
    0  => array("TransDate" => "2019-10-28 09:35:14", "Amount" => "12.00"),
);

第三塊

//index 1 through index 4, they all are under 60 minutes difference from index 0
array(
    0  => array("TransDate" => "2019-10-28 10:50:14", "Amount" => "12.00"),
    1  => array("TransDate" => "2019-10-28 10:58:14", "Amount" => "12.00"),
    2  => array("TransDate" => "2019-10-28 10:58:14", "Amount" => "12.00"),
    3  => array("TransDate" => "2019-10-28 11:40:14", "Amount" => "12.00"),
    4  => array("TransDate" => "2019-10-28 11:49:14", "Amount" => "12.00")
);

第四塊

//There are no elements that is under 60 minute difference from first element below, 
//on that particular day 28th October, we'll have just one element.
array(
    0 => array("TransDate" => "2019-10-28 23:50:14", "Amount" => "12.00")
);

任何人都可以幫助我如何實現這一目標? 謝謝

您可以通過簡單地增加一個計數器以用作結果數組的第一級上的索引來做到這一點 - 然后您只需將項目附加到該計數器下的數組中。

每次計數器增加時,您還可以將要比較的開始日期設置為當前項目的開始日期,以便它標記當前間隔的開始。

$output = [];
$c = -1;
$start = null;

foreach($input as $item) {
  if(!$start || strtotime($item['TransDate']) - $start > 60*60) {
    $c++;  
    $start = strtotime($item['TransDate']);
  }
  $output[$c][] = $item;
}

演示: https : //3v4l.org/12TFF

與僅使用 strtotime 並將差異與 3600 秒進行比較(例如,可能會導致 DST 出現問題)相比,實際的時間戳比較可能會使用更多的技巧 - 但基本原理就是這么簡單。

您的輸入數組當然需要按升序開始日期正確排序,如果這不是給定的,請確保您先這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM