如何根据php中的特定值将数组拆分为子数组

时间:2011-03-04 07:58:24

标签: php arrays

我有一个如下所示的数组,我想要做的是,创建基于datepart的多个数组,如(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
        )

    [11] => Array
        (
            [avgvalue] => 268.908333333
            [maxvalue] => 269
            [minvalue] => 268.9
            [nrsamples] => 0.0288675134595
            [stddeviation] => 0.0288675134595
            [timestamp] => 2011-02-04T22:00:00.000Z
        )

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

    [13] => Array
        (
            [avgvalue] => 269.05
            [maxvalue] => 269.1
            [minvalue] => 269
            [nrsamples] => 0.0522232967867
            [stddeviation] => 0.0522232967867
            [timestamp] => 2011-02-05T00:00:00.000Z
        )

    [14] => Array
        (
            [avgvalue] => 269.1
            [maxvalue] => 269.1
            [minvalue] => 269.1
            [nrsamples] => 0
            [stddeviation] => 0
            [timestamp] => 2011-02-05T01:00:00.000Z
        )

    [15] => Array
        (
            [avgvalue] => 269.166666667
            [maxvalue] => 269.2
            [minvalue] => 269.1
            [nrsamples] => 0.0492365963917
            [stddeviation] => 0.0492365963917
            [timestamp] => 2011-02-05T02:00:00.000Z
        )

    [16] => Array
        (
            [avgvalue] => 269.2
            [maxvalue] => 269.2
            [minvalue] => 269.2
            [nrsamples] => 0
            [stddeviation] => 0
            [timestamp] => 2011-02-05T03:00:00.000Z
        )

    [17] => Array
        (
            [avgvalue] => 269.291666667
            [maxvalue] => 269.3
            [minvalue] => 269.2
            [nrsamples] => 0.0288675134595
            [stddeviation] => 0.0288675134595
            [timestamp] => 2011-02-05T04:00:00.000Z
        )

    [18] => Array
        (
            [avgvalue] => 269.333333333
            [maxvalue] => 269.4
            [minvalue] => 269.3
            [nrsamples] => 0.0492365963917
            [stddeviation] => 0.0492365963917
            [timestamp] => 2011-02-05T05:00:00.000Z
        )

    [19] => Array
        (
            [avgvalue] => 269.4
            [maxvalue] => 269.4
            [minvalue] => 269.4
            [nrsamples] => 0
            [stddeviation] => 0
            [timestamp] => 2011-02-05T06: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)

被困在这里,无法使其发挥作用。请建议。

2 个答案:

答案 0 :(得分:7)

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

$result = array();

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

参考:strstrarray_key_exists

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

答案 1 :(得分:0)

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

解决方案很简单:(Demo

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

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