简体   繁体   English

从数组php中查找连续的日期时间

[英]Find consecutive datetime from array php

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,抱歉我的英语,我是 PHP 新手,我是 PHP 新手。我有如下数组,我试图在 10 分钟内为每个用户查找连续日期时间的计数,

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我试图在 10 分钟内找到用户点击的连续日期时间计数,比如用户 A 在 10 分钟内连续点击 4 次,而用户 B 在 10 分钟内仅点击 1 次

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

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

        )
)

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.您可以将一个函数与您的数组和要算作参数的$user一起使用,然后循环到数组中并为每个符合您的先决条件的交互求和 +1。

Assuming your parent array is named $users , you can do the following:假设您的父数组名为$users ,您可以执行以下操作:

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.该函数的返回将是用户在过去 10 分钟内连续日期时间的计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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