2

Sorry for my English ,I am new here and new to PHP.I have array like below,I was trying find count of consecutive date-time from for every user within 10 min,

Array
(
    [0] => Array
        (
            [user] => A
            [time] => 2018-08-01 14:12:00

        )
    [1] => Array
        (
            [user] => B
            [time] => 2018-08-01 14:12:00

        )
    [2] => Array
        (

            [user] => A
            [time] => 2018-08-01 14:13:00

        )
    [3] => Array
        (

            [user] => A
            [time] => 2018-08-01 14:14:00

        )
    [4] => Array
        (

            [user] => A
            [time] => 2018-08-01 14:15:00

        )
    [5] => Array
        (

            [user] => B
            [time] => 2018-08-01 14:50:00

        )
)

I was trying to find consecutive datetime count of user click within 10 min,Like user A click 4 times consecutively within 10 min and User B click only 1 time within 10 min

Array
(
    [0] => Array
        (
            [user] => A
            [count] => 4

        )
    [1] => Array
        (
            [user] => B
            [count] => 1

        )
)
3
  • do you have this data in MySQL? If yes, you can do that in MySQL itself. If not, that do two loops to get that data. Commented Sep 13, 2019 at 19:56
  • Also you're talking about any 10 min range or last 10 minutes? Commented Sep 13, 2019 at 19:57
  • 1
    The task requirements are not very clear and the sample isn't sufficiently challenging. We don't know if you want the most clicks within any 10 period per user. Do we need to check every possible 10 minute set of records for each user? Do qualifying records need to be consecutive or just within the 10 minute period? Commented Oct 3, 2024 at 22:33

1 Answer 1

2

You can use a function with your array and the $user you want to count as parameter, and then loop into the array and sum +1 for each interaction that matches your prerequisites.

Assuming your parent array is named $users, you can do the following:

function countConsecutive($users, $username)
{
    $total = 0;

    foreach($users as $user) {
        if($user['user'] === $username && $user['time'] >= date('Y-m-d H:i:s', strtotime('-10 minutes'))) {
            $total++;
        }
    }

    return $total;
}

The return of the function will be the count of consecutive date-time from the user in the last 10 minutes.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.