skip to Main Content

I have result in array like this

Array
(
    [2023-06-09 10:21:48] => Array
        (
            [count] => 0
        )


    [2023-06-09 10:21:50] => Array
        (
            [count] => 1
        )

    [2023-06-09 10:21:51] => Array
        (
            [count] => 0
        )

    [2023-06-09 10:21:52] => Array
        (
            [count] => 1
        )
...
    [2023-06-09 10:22:53] => Array
        (
            [count] => 3
        )
...
    [2023-06-09 10:23:54] => Array
        (
            [count] => 1
        )
)

But i need to group that result in another foreach and SUM values (count) so result should be

Array
(
    [2023-06-09 10:21] => Array
        (
            [count] => 2
        )


    [2023-06-09 10:22] => Array
        (
            [count] => 3
        )

    [2023-06-09 10:23] => Array
        (
            [count] => 1
        )
)

So basically i need to group result by time and minutes. This is what I need but how to sum values and group tham every minutes ?

        foreach ($array as $key => $value) {
            $timeFirst  = strtotime(date("Y-m-d H:i",strtotime($key)));
            $timeSecond = strtotime(date("Y-m-d H:i"));
            $final = ($timeSecond - $timeFirst) / 60;
            $finalArray[$final.' minutes ago']  = $value;
        }

2

Answers


  1. A simple foreach loop and the use of the DateTime builtin class to make the change to the time should do this nicely

    $array = [
        '2023-06-09 10:21:48' => ['count' => 0] ,
        '2023-06-09 10:21:50' => ['count' => 1] ,
        '2023-06-09 10:21:51' => ['count' => 0] ,
        '2023-06-09 10:21:52' => ['count' => 1] ,
        '2023-06-09 10:22:53' => ['count' => 3] ,
        '2023-06-09 10:23:54' => ['count' => 1] 
    ];
    
    $newArray = [];
    foreach ($array as $key => $arr) {
        # reformat the time, removing the seconds
        $t = (new DateTime($key))->format('Y-m-d H:i');
    
        if ( array_key_exists( $t, $newArray ) ) {
            // already found this date/time so add count
            $newArray[$t]['count'] +=  $arr['count'];
        } else {
            $newArray[$t]['count'] =  $arr['count'];
        }
    
    }
    print_r($newArray);
    

    And the results

    Array
    (
        [2023-06-09 10:21] => Array ( [count] => 2  )
        [2023-06-09 10:22] => Array ( [count] => 3 )
        [2023-06-09 10:23] => Array ( [count] => 1 )
    )
    

    Of course instead of using DateTime to remove the seconds like this

    $t = (new DateTime($key))->format('Y-m-d H:i');
    

    you could simply chop off the las 3 characters of the date and time like this

    $t = substr($key, 0, -3);
    
    Login or Signup to reply.
  2. It’s not clear if you want the result like the section ‘so result should be’ or like the code you posted "$finalArray[$final.’ minutes ago’]

    For the first option you can simply strip the seconds:

    $finalArray = [];
    
    foreach ($array as $key => $value) {
       $newKey = substr ($key, 0, 16);
       $finalArray[$newKey]['count'] = ($finalArray[$newKey]['count'] ?? 0) + $value['count'];
    }
    

    EDIT

    For your request on the past minutes, I suggest you run a further cycle, taking advantage of the fact that the rows to be processed will have decreased:

    $veryFinalArray = [];
    $now = new DateTime();
    
    foreach ($finalArray as $key => $value) {
        
        $diff = $now->diff (new DateTime($key));
        
        $minutesKey = $diff->days * 24 * 60 +
                      $diff->h * 60 +
                      $diff->i;
                      
        $veryFinalArray[$minutesKey] = $value;
    }
    

    I haven’t added the ‘minutes ago’ string because it’s ever the same, evaluate yourself if it’s needed.

    Another interesting option can be adding a value to the previous array instead of using the $veryFinalArray:

    $now = new DateTime();
    
    foreach ($finalArray as $key => $value) {
        
        $diff = $now->diff (new DateTime($key));
        
        $minutes = $diff->days * 24 * 60 +
                   $diff->h * 60 +
                   $diff->i;
                      
        $finalArray[$key]['elapsed_minutes'] = $minutes;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search