Home > database >  Group PHP array by date & time
Group PHP array by date & time

Time:05-05

I have an array which I am trying to organise in a way easier to parse later on in another application. The output will be printed as JSON. The initial array is shown below:

    Array
(
    [0] => Array
        (
            [index] => 0
            [timestamp] => 2022-05-04 02:45:09 BST
            [date] => 2022-05-04
            [time] => 02:45:09
            [type] => Low
            [height] => 1.6m
            [diff] => 1day 10hrs
        )

    [1] => Array
        (
            [index] => 1
            [timestamp] => 2022-05-04 08:52:57 BST
            [date] => 2022-05-04
            [time] => 08:52:57
            [type] => High
            [height] => 7.7m
            [diff] => 1day 4hrs
        )

    [2] => Array
        (
            [index] => 2
            [timestamp] => 2022-05-04 14:56:59 BST
            [date] => 2022-05-04
            [time] => 14:56:59
            [type] => Low
            [height] => 1.7m
            [diff] => 21hrs 56mins
        )

    [3] => Array
        (
            [index] => 3
            [timestamp] => 2022-05-04 21:04:45 BST
            [date] => 2022-05-04
            [time] => 21:04:45
            [type] => High
            [height] => 7.7m
            [diff] => 15hrs 49mins
        )

    [4] => Array
        (
            [index] => 4
            [timestamp] => 2022-05-05 03:14:44 BST
            [date] => 2022-05-05
            [time] => 03:14:44
            [type] => Low
            [height] => 1.9m
            [diff] => 9hrs 39mins
        )

    [5] => Array
        (
            [index] => 5
            [timestamp] => 2022-05-05 09:24:25 BST
            [date] => 2022-05-05
            [time] => 09:24:25
            [type] => High
            [height] => 7.4m
            [diff] => 3hrs 29mins
        )

    [6] => Array
        (
            [index] => 6
            [timestamp] => 2022-05-05 15:25:35 BST
            [date] => 2022-05-05
            [time] => 15:25:35
            [type] => Low
            [height] => 2.1m
            [diff] => 2hrs 31mins
        )

    )

I would like to know if it is possible to group this data by date, time & type? something like the below (not correct syntax I know)

Array
    (
        [0] => Array
            (
                [2022-05-04] => Array
            ( 
                [02:45:09] => Array
                    (

                         [index] => 0
                             [type] => Low
                             [height] => 1.6m
                             [diff] => 1day 10hrs
                    )
                [08:52:57] => Array
                    (

                         [index] => 1
                             [type] => High
                             [height] => 7.7m
                             [diff] => 1day 4hrs
                    )
                    [14:56:59] => Array
                    (

                         [index] => 2
                             [type] => Low
                             [height] => 1.7m
                             [diff] => 21hrs 56mins
                    )
                [21:04:45] => Array
                    (

                         [index] => 3
                             [type] => High
                             [height] => 7.7m
                             [diff] => 15hrs 49mins
                    )
                )
            [2022-05-05] => Array
                    (
                [03:14:44] => Array
                    (
                         [index] => 4
                             [type] => Low
                             [height] => 1.9m
                             [diff] => 1day 10hrs
                    )
                [09:24:25] => Array
                    (

                         [index] => 5
                             [type] => High
                             [height] => 7.4m
                             [diff] => 3hrs 29mins
                    )
                    [15:25:35] => Array
                    (

                         [index] => 6
                             [type] => Low
                             [height] => 2.1m
                             [diff] => 2hrs 31mins
                    )
                )

I have tried the below but am a ways off

$output=[];
$times=array();
$count2 = 0;
foreach($array as $values)
{
 $d=date("Y-m-d",strtotime($values["timestamp"]));
 
 $t=date("H:i:s",strtotime($values["timestamp"]));

 $output[$d] = [$t];
}

CodePudding user response:

$newArray = [];
$allowedKeys = ['type','height','diff'];

foreach($array as $values) {
    $tempArray = [];
    foreach($values as $k => $v) {
        if(in_array($k, $allowedKeys)) {
            $tempArray[$k] = $v
        }
    }
    $newArray[$values['date']][] = $tempArray;
}

CodePudding user response:

$newArray = [];
$allowedKeys = ['index','type','height','diff'];

foreach($array as $values2) {
    foreach($values2 as $k => $v) {
        if(in_array($k, $allowedKeys)) {
            $newArray[$values2['date']][$values2['time']][$k] = $v;
        }
    }
}

or

$output=[];
foreach($array as $values)
{
 $d=date("Y-m-d",strtotime($values["timestamp"]));
 
 $t=date("H:i:s",strtotime($values["timestamp"]));

 $output[$d][$t]=["index" => $values["index"], "type" => $values["type"], "height" => $values["height"], "diff" => $values["diff"] ];
}

seem to do the job. Thanks

  • Related