简体   繁体   English

创建一个数组,该数组根据DateTime间隔对象php的大小在其内部创建较小的变量数组

[英]Creating an array that creates smaller variable arrays within itself based on the size of a DateTime interval object php

This is somewhat basic but one of my first PHP projects and I'm stuck. 这有点基本,但是是我的第一个PHP项目之一,所以我陷入了困境。 I need to create something that will iterate through a series of DateTime objects and sort them into groups based on the interval of time between each object, with the end goal being to average data associated with these groups of dates (just focusing on how to group the data in this post). 我需要创建一些东西来迭代一系列DateTime对象,并根据每个对象之间的时间间隔将它们分类为组,最终目标是平均与这些日期组相关的数据(仅关注如何分组)这篇文章中的数据)。 If the DateTime objects are more than 1 day apart, then they need to be in a separate sub-group but still a part of the larger list of dates. 如果DateTime对象相隔超过1天,则它们需要位于单独的子组中,但仍是较大日期列表的一部分。

I have been able to create DateInterval objects from the DateTime objects. 我已经能够从DateTime对象创建DateInterval对象。 Below is an example of two DateTime objects pulled from a larger list of dates and the corresponding DateInterval generated from the amount of time between the two` 下面是从较大的日期列表中提取两个DateTime对象的示例,以及从两者之间的时间量生成的相应DateInterval

[4] => DateTime Object
    (
        [date] => 2017-04-21 12:50:53.000000
        [timezone_type] => 3
        [timezone] => America/Denver
    )

[5] => DateTime Object
    (
        [date] => 2015-04-18 12:50:53.000000
        [timezone_type] => 3
        [timezone] => America/Denver
    )


    `[9] => DateInterval Object
    (
        [y] => 2
        [m] => 0
        [d] => 3
        [h] => 0
        [i] => 0
        [s] => 0
        [weekday] => 0
        [weekday_behavior] => 0
        [first_last_day_of] => 0
        [invert] => 1
        [days] => 734
        [special_type] => 0
        [special_amount] => 0
        [have_weekday_relative] => 0
        [have_special_relative] => 0
    )`

Is there a way I could create some sort of array that holds subgroups of date objects in their own array that are sorted based on the size of their intervals? 有没有一种方法可以创建某种类型的数组,以将日期对象的子组保存在其自己的数组中,这些对象组根据其间隔的大小进行排序? I illustrated the structure of what I'm thinking below. 我在下面说明了我的想法。

Array [
       Array [
                [date1],
                [date2],
                [date3]
             ]
       Array [ 
                [date4],
                [date5],
             ]
       Array [
                [date6]
             ]
       ]

First, sort your array of dates (if they aren't already sorted). 首先,对日期数组进行排序(如果尚未排序)。

sort($dates);

Then keep track of the previous item as you iterate the array. 然后在迭代数组时跟踪上一项。 If the number of days in the diff interval exceeds your threshold then increment the key used to define the groups in the result array. 如果差异间隔中的天数超过了阈值,则增加用于定义结果数组中组的键。

$i = 0;
$previous = null;

foreach ($dates as $date) {
    if ($previous && $date->diff($previous)->days > 1) {
        $i++;
    }
    $groups[$i][] = $date;
    $previous = $date;
}

This is the code that solved my problem, which was made from a slight modification of @Don'tPanic's code. 这是解决我的问题的代码,它是通过对@ Don'tPanic的代码进行一些修改而制成的。

$incrementKey = 0;
foreach ($datesIntervalArray as $d) {

    if ($d->days > 1) {
        $incrementKey++;
    }

    $groupsOfVisits[$incrementKey][] = $d;

`

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

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