Jump to content

Group Timestamps in array into unique instances and count them via php


Go to solution Solved by Barand,

Recommended Posts

I have an array that looks like this:

 

array(5642) {

[0]=> string(19) "2021-02-10 09:04:48"

[1]=> string(19) "2021-02-10 09:04:54"

[2]=> string(19) "2021-02-10 09:05:00"

[3]=> string(19) "2021-02-10 09:05:06"

[4]=> string(19) "2021-02-10 09:05:12"

[5]=> string(19) "2021-02-10 09:05:18"

[6]=> string(19) "2021-02-10 09:06:18"

[7]=> string(19) "2021-02-10 09:06:24"

}

 

I need to group the instances any time there is more than a 6 second gap between elements. So 0 =>5 would be one array and 6 and 7 would be a new array.

 

Ive been able to produce the following but its not ideal

 

array(5642) {

[0]=> string(19) "2021-02-10 09:04:48"

[1]=> string(19) "2021-02-10 09:04:54"

[2]=> string(19) "2021-02-10 09:05:00"

[3]=> string(19) "2021-02-10 09:05:06"

[4]=> string(19) "2021-02-10 09:05:12"

[5]=> string(19) "2021-02-10 09:05:18"

[6]=> string(19) "end"

[7]=> string(19) "2021-02-10 09:06:18"

[8]=> string(19) "2021-02-10 09:06:24" }

 

$burner_time = array();

foreach($Burner_Control_Alm as $new_burner){ $burner_time[] = strtotime($new_burner); }

$repl = 'end'; for ($i=1; $i<count($burner_time); $i++) { $value_second = $burner_time[$i]; $value_first = $burner_time[$i-1] === $repl ? $burner_time[$i-2] : $burner_time[$i-1]; if ($value_second > $value_first + 6) {

array_splice($burner_time, $i++, 0, $repl);

}

print_r($burner_time);

}

  • Solution

try

foreach ($array as $k => $d) {
     if ($k > 0) {
         if (strtotime($d) > strtotime($array[$k-1])+6) {
             $new[] = "-------------------";
         }
     }
     $new[] = $d;
}

 

$new = Array
(
    [0] => 2021-02-10 09:04:48
    [1] => 2021-02-10 09:04:54
    [2] => 2021-02-10 09:05:00
    [3] => 2021-02-10 09:05:06
    [4] => 2021-02-10 09:05:12
    [5] => 2021-02-10 09:05:18
    [6] => -------------------
    [7] => 2021-02-10 09:06:18
    [8] => 2021-02-10 09:06:24
)

[edit...]

Alternative solution...

$new = [];
$newkey = 0;

foreach ($array as $k => $d) {
     if ($k > 0) {
         if (strtotime($d) > strtotime($array[$k-1])+6) {
             $newkey++;
         }
     }
     $new[$newkey][] = $d;
}

gives

$new = Array
(
    [0] => Array
        (
            [0] => 2021-02-10 09:04:48
            [1] => 2021-02-10 09:04:54
            [2] => 2021-02-10 09:05:00
            [3] => 2021-02-10 09:05:06
            [4] => 2021-02-10 09:05:12
            [5] => 2021-02-10 09:05:18
        )

    [1] => Array
        (
            [0] => 2021-02-10 09:06:18
            [1] => 2021-02-10 09:06:24
        )

)

 

Edited by Barand
  • Like 1
  • Great Answer 1

Barand You are the Best! I know sometimes I run in to an end and the solution is there but just hard to find after staring at the same code for days, you my friend are a great start to my weekend! Best to you. And just so you know I posted this on Stack Overflow and was told that I did not try enough and i should try harder before asking questions. I will speak highly of this form and stick around to see if I can help others as well. 

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.