简体   繁体   English

根据子数组中的日期拆分多维数组

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

I have following multidimensional array of dates我有以下多维日期数组

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

Requirement .要求

The above array is needed to be splitted into chunks so that each chunk will have elements that have TransDate within a 60 minutes.需要将上述数组拆分为多个块,以便每个块都具有在 60 分钟内具有TransDate元素。

the following chunks will throw more light on what actually is needed as a result.以下块将更清楚地说明实际需要的结果。

Needed Result需要的结果

First Chunk第一块

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

Second Chunk第二块

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

Third Chunk第三块

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

Fourth Chunk第四块

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

Can anybody help me on this how to achieve this??任何人都可以帮助我如何实现这一目标? Thanks谢谢

You can do this by simply increasing a counter to use as index on your first level of the result array - and then you simply append the items to the array under that counter.您可以通过简单地增加一个计数器以用作结果数组的第一级上的索引来做到这一点 - 然后您只需将项目附加到该计数器下的数组中。

Every time that counter gets increased, you also set the start date to compare to, to the current item's start date, so that it marks the start of the current interval.每次计数器增加时,您还可以将要比较的开始日期设置为当前项目的开始日期,以便它标记当前间隔的开始。

$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;
}

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

The actual timestamp comparison could probably use a little more finesse than just using strtotime and comparing the difference to 3600 seconds (might lead into trouble with DST for example) - but the basic principle is just that simple.与仅使用 strtotime 并将差异与 3600 秒进行比较(例如,可能会导致 DST 出现问题)相比,实际的时间戳比较可能会使用更多的技巧 - 但基本原理就是这么简单。

Your input array of course needs to be correctly sorted by ascending start dates already, if that's not a given, see to it that you do that first.您的输入数组当然需要按升序开始日期正确排序,如果这不是给定的,请确保您先这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM