按列值的前导字符对数据行进行分组

发布于 2024-10-20 03:47:36 字数 3630 浏览 0 评论 0原文

我有一个如下所示的数组,我想要做的是根据 timestamp 列的日期子字符串创建多个子数组,例如 (2011-02-04)

Array
(
    [0] => Array
        (
            [avgvalue] => 0
            [maxvalue] => 0
            [minvalue] => 0
            [nrsamples] => 0
            [stddeviation] => 0
            [timestamp] => 2011-02-04T11:00:00.000Z
        )

    [1] => Array
        (
            [avgvalue] => 268.3
            [maxvalue] => 268.3
            [minvalue] => 268.3
            [nrsamples] => 0
            [stddeviation] => 0
            [timestamp] => 2011-02-04T12:00:00.000Z
        )

    [2] => Array
        (
            [avgvalue] => 268.391666667
            [maxvalue] => 268.4
            [minvalue] => 268.3
            [nrsamples] => 0.0288675134595
            [stddeviation] => 0.0288675134595
            [timestamp] => 2011-02-04T13:00:00.000Z
        )

    [3] => Array
        (
            [avgvalue] => 268.433333333
            [maxvalue] => 268.5
            [minvalue] => 268.4
            [nrsamples] => 0.0492365963918
            [stddeviation] => 0.0492365963918
            [timestamp] => 2011-02-04T14:00:00.000Z
        )

    [4] => Array
        (
            [avgvalue] => 268.5
            [maxvalue] => 268.5
            [minvalue] => 268.5
            [nrsamples] => 0
            [stddeviation] => 0
            [timestamp] => 2011-02-04T15:00:00.000Z
        )

    [5] => Array
        (
            [avgvalue] => 268.575
            [maxvalue] => 268.6
            [minvalue] => 268.5
            [nrsamples] => 0.0452267016867
            [stddeviation] => 0.0452267016867
            [timestamp] => 2011-02-04T16:00:00.000Z
        )

    [6] => Array
        (
            [avgvalue] => 268.616666667
            [maxvalue] => 268.7
            [minvalue] => 268.6
            [nrsamples] => 0.0389249472081
            [stddeviation] => 0.0389249472081
            [timestamp] => 2011-02-04T17:00:00.000Z
        )

    [7] => Array
        (
            [avgvalue] => 268.7
            [maxvalue] => 268.7
            [minvalue] => 268.7
            [nrsamples] => 0
            [stddeviation] => 0
            [timestamp] => 2011-02-04T18:00:00.000Z
        )

    [8] => Array
        (
            [avgvalue] => 268.741666667
            [maxvalue] => 268.8
            [minvalue] => 268.7
            [nrsamples] => 0.0514928650545
            [stddeviation] => 0.0514928650545
            [timestamp] => 2011-02-04T19:00:00.000Z
        )

    [9] => Array
        (
            [avgvalue] => 268.8
            [maxvalue] => 268.8
            [minvalue] => 268.8
            [nrsamples] => 0
            [stddeviation] => 0
            [timestamp] => 2011-02-04T20:00:00.000Z
        )

    [10] => Array
        (
            [avgvalue] => 268.883333333
            [maxvalue] => 268.9
            [minvalue] => 268.8
            [nrsamples] => 0.0389249472081
            [stddeviation] => 0.0389249472081
            [timestamp] => 2011-02-04T21:00:00.000Z
        )
 )

:上面的数组在每个子数组中都有一个时间戳键。我分解了时间戳值以将日期与时间分开,现在我无法将数组拆分为子数组。

我想要的是有一个 2011-02-04 数组(包含该日期的所有值)和另一个 2011-02-05 数组(包含所有那个日期)。这可以是动态的,我的意思是日期可以更多。那么,我该怎么做呢?

我希望它是:

array[0] => array(... list of all the values for 2011-02-04),
array[1] => array(...list of all values for 2011-02-05)

I have an array as shown below, what I wanted to do is create multiple subarrays based on the date substring of the timestamp column like (2011-02-04):

Array
(
    [0] => Array
        (
            [avgvalue] => 0
            [maxvalue] => 0
            [minvalue] => 0
            [nrsamples] => 0
            [stddeviation] => 0
            [timestamp] => 2011-02-04T11:00:00.000Z
        )

    [1] => Array
        (
            [avgvalue] => 268.3
            [maxvalue] => 268.3
            [minvalue] => 268.3
            [nrsamples] => 0
            [stddeviation] => 0
            [timestamp] => 2011-02-04T12:00:00.000Z
        )

    [2] => Array
        (
            [avgvalue] => 268.391666667
            [maxvalue] => 268.4
            [minvalue] => 268.3
            [nrsamples] => 0.0288675134595
            [stddeviation] => 0.0288675134595
            [timestamp] => 2011-02-04T13:00:00.000Z
        )

    [3] => Array
        (
            [avgvalue] => 268.433333333
            [maxvalue] => 268.5
            [minvalue] => 268.4
            [nrsamples] => 0.0492365963918
            [stddeviation] => 0.0492365963918
            [timestamp] => 2011-02-04T14:00:00.000Z
        )

    [4] => Array
        (
            [avgvalue] => 268.5
            [maxvalue] => 268.5
            [minvalue] => 268.5
            [nrsamples] => 0
            [stddeviation] => 0
            [timestamp] => 2011-02-04T15:00:00.000Z
        )

    [5] => Array
        (
            [avgvalue] => 268.575
            [maxvalue] => 268.6
            [minvalue] => 268.5
            [nrsamples] => 0.0452267016867
            [stddeviation] => 0.0452267016867
            [timestamp] => 2011-02-04T16:00:00.000Z
        )

    [6] => Array
        (
            [avgvalue] => 268.616666667
            [maxvalue] => 268.7
            [minvalue] => 268.6
            [nrsamples] => 0.0389249472081
            [stddeviation] => 0.0389249472081
            [timestamp] => 2011-02-04T17:00:00.000Z
        )

    [7] => Array
        (
            [avgvalue] => 268.7
            [maxvalue] => 268.7
            [minvalue] => 268.7
            [nrsamples] => 0
            [stddeviation] => 0
            [timestamp] => 2011-02-04T18:00:00.000Z
        )

    [8] => Array
        (
            [avgvalue] => 268.741666667
            [maxvalue] => 268.8
            [minvalue] => 268.7
            [nrsamples] => 0.0514928650545
            [stddeviation] => 0.0514928650545
            [timestamp] => 2011-02-04T19:00:00.000Z
        )

    [9] => Array
        (
            [avgvalue] => 268.8
            [maxvalue] => 268.8
            [minvalue] => 268.8
            [nrsamples] => 0
            [stddeviation] => 0
            [timestamp] => 2011-02-04T20:00:00.000Z
        )

    [10] => Array
        (
            [avgvalue] => 268.883333333
            [maxvalue] => 268.9
            [minvalue] => 268.8
            [nrsamples] => 0.0389249472081
            [stddeviation] => 0.0389249472081
            [timestamp] => 2011-02-04T21:00:00.000Z
        )
 )

The above array has a timestamp key in each subarray. I exploded the timestamp value to separate the date from the time, now I am having trouble splitting the array into sub-arrays.

What I wanted was to have one array for 2011-02-04 (containing all the values for that date) and another array for 2011-02-05 (containing values for all that date). This can be dynamic, I mean the dates can be even more. So, how can I do that?

I want it as:

array[0] => array(... list of all the values for 2011-02-04),
array[1] => array(...list of all values for 2011-02-05)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独自←快乐 2024-10-27 03:47:36

假设所有条目的日期格式都相同(看起来是这样),您可以简单地循环数组:

$result = array();

foreach($array as $item) {
    $date = strstr($item['timestamp'], 'T', true);
    if(!array_key_exists($date, $result)) {
        $result[$date] = array();
    }
    $result[$date][] = $item;
}

参考:strstr, array_key_exists

根据原始数组中项目的顺序,您可能必须使用 ksort 按时间顺序对 $result 数组进行排序。

Assuming the date format is the same for all entries (which it appears to be), you can simply loop over the array:

$result = array();

foreach($array as $item) {
    $date = strstr($item['timestamp'], 'T', true);
    if(!array_key_exists($date, $result)) {
        $result[$date] = array();
    }
    $result[$date][] = $item;
}

Reference: strstr, array_key_exists

Depending on the order of the items in your original array, you might have to use ksort to sort the $result array chronologically.

绝不放开 2024-10-27 03:47:36

在推送新子数组之前无需检查数组键是否存在。只需将子数组推入日期键控子数组即可。

解决方案很简单:(Demo)

$result = [];
foreach ($array as $row) {
    $result[substr($row['timestamp'], 0, 10)][] = $row;
}

分组后,如果要对组进行排序,请使用 ksort($result) 用于 ASC 或 krsort($result) 用于 DESC。如果您想重新索引第一级键,请使用$result = array_values($result)

There is no need to check if an array key exists before pushing a new subarray. Just push the subarray into the date-keyed subarray.

The solution is as simple as: (Demo)

$result = [];
foreach ($array as $row) {
    $result[substr($row['timestamp'], 0, 10)][] = $row;
}

After grouping, if you want to sort the groups, use ksort($result) for ASC or krsort($result) for DESC. If you want to reindex the first level keys, use $result = array_values($result).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文