简体   繁体   中英

Group rows of data by the leading characters of a column value

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)

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.

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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