Php 检查CSV是否有两行具有相同文本(&;60分钟内的时间

Php 检查CSV是否有两行具有相同文本(&;60分钟内的时间,php,csv,datetime,Php,Csv,Datetime,我正在循环浏览一个具有以下结构的CSV文件 text,time "Hey you",20181219T15:59:00 "Hey you",20181219T15:39:00 "Random",20181219T15:39:00 它只包含一个“文本字符串”和另一个表示ISO 8601日期的字符串 我想执行一个功能,检查a)它是否找到了两行完全相同的文本,这两行之间的时间间隔是否在60分钟之内 任何人都可以建议最好的方法来实现这一点(考虑到CSV将最多有50个条目,因此希望尽可能提高效率 因此,

我正在循环浏览一个具有以下结构的CSV文件

text,time
"Hey you",20181219T15:59:00
"Hey you",20181219T15:39:00
"Random",20181219T15:39:00
它只包含一个“文本字符串”和另一个表示ISO 8601日期的字符串

我想执行一个功能,检查a)它是否找到了两行完全相同的文本,这两行之间的时间间隔是否在60分钟之内

任何人都可以建议最好的方法来实现这一点(考虑到CSV将最多有50个条目,因此希望尽可能提高效率

因此,通过DateTime解析日期后,返回的数组如下所示

Array
(
[0] => Array
    (
        [text] => Hey you
        [time] => DateTime Object
            (
                [date] => 2018-12-19 15:59:00.000000
                [timezone_type] => 3
                [timezone] => Europe/London
            )

    )

[1] => Array
    (
        [text] => Hey you
        [time] => DateTime Object
            (
                [date] => 2018-12-19 15:39:00.000000
                [timezone_type] => 3
                [timezone] => Europe/London
            )

    )
)

检查完全相同的文本和时间在同一文本一小时内的最佳方法是什么?

假设数组按日期时间排序(因此较大的键表示较新的日期时间),您可以执行以下操作:

// set an array to track text we meet
$textList = [];
foreach ($array as $key => $element){
    $text = $element['text'];
    if (!array_key_exists($text, $textList)) {
        // first time we meet this text, we track it and its position in the array
        $textList[$text] = $key;
    }else{
        // second time we meet this test, we compare the current date time with the previous one to get difference in minutes
        $currentTime = $element['time'];
        $previousTimeKey = $textList[$text];
        $previousTime = $array[$previousTimeKey]['time'];
        $diff = $currentTime->diff($previousTime);
        // total minutes of diff: hours*60 + minutes
        $diffInMinutes = $diff->format('%h') * 60 + $diff->format('%i');
        if ($diffInMinutes < 60) {
            // do whatever you need.. 
        }
    }
}
//设置一个数组来跟踪我们遇到的文本
$textList=[];
foreach($key=>$element的数组){
$text=$element['text'];
如果(!array_key_存在($text,$textList)){
//第一次遇到这个文本时,我们跟踪它及其在数组中的位置
$textList[$text]=$key;
}否则{
//第二次进行此测试时,我们将当前日期时间与前一个日期时间进行比较,以获得以分钟为单位的差异
$currentTime=$element['time'];
$previousTimeKey=$textList[$text];
$previousTime=$array[$previousTimeKey]['time'];
$diff=$currentTime->diff($previousTime);
//差异总分钟数:小时*60+分钟
$diffInMinutes=$diff->format('%h')*60+$diff->format('%i');
如果($diffInMinutes<60){
//你需要什么就做什么。。
}
}
}
为满足您的特殊需要,将其复杂化


PS:如果数组不是按日期时间排序的,则考虑先对其进行排序,然后使用该函数:不按日期时间排序的数组的算法将更加困难。

假定数组按日期时间排序(因此,较大的密钥意味着更新的日期时间),则可以如下:

// set an array to track text we meet
$textList = [];
foreach ($array as $key => $element){
    $text = $element['text'];
    if (!array_key_exists($text, $textList)) {
        // first time we meet this text, we track it and its position in the array
        $textList[$text] = $key;
    }else{
        // second time we meet this test, we compare the current date time with the previous one to get difference in minutes
        $currentTime = $element['time'];
        $previousTimeKey = $textList[$text];
        $previousTime = $array[$previousTimeKey]['time'];
        $diff = $currentTime->diff($previousTime);
        // total minutes of diff: hours*60 + minutes
        $diffInMinutes = $diff->format('%h') * 60 + $diff->format('%i');
        if ($diffInMinutes < 60) {
            // do whatever you need.. 
        }
    }
}
//设置一个数组来跟踪我们遇到的文本
$textList=[];
foreach($key=>$element的数组){
$text=$element['text'];
如果(!array_key_存在($text,$textList)){
//第一次遇到这个文本时,我们跟踪它及其在数组中的位置
$textList[$text]=$key;
}否则{
//第二次进行此测试时,我们将当前日期时间与前一个日期时间进行比较,以获得以分钟为单位的差异
$currentTime=$element['time'];
$previousTimeKey=$textList[$text];
$previousTime=$array[$previousTimeKey]['time'];
$diff=$currentTime->diff($previousTime);
//差异总分钟数:小时*60+分钟
$diffInMinutes=$diff->format('%h')*60+$diff->format('%i');
如果($diffInMinutes<60){
//你需要什么就做什么。。
}
}
}
为满足您的特殊需要,将其复杂化


PS:如果数组不是按日期时间排序的,考虑先对其进行排序,然后使用此函数:不按日期时间排序的数组的算法会更加困难。

用给定数组尝试这个。 我对时差的解决方案可能不太好,但我注意到另一个答案并不会在时差中花费天/月/年,因此如果日期不同,但一天中的时间小于60分钟,它就会过去

无论第一个日期是更老还是更年轻,这种方法都有效,因此不需要数组排序

function findMatch($arrays){
$tmp_list = []; //takes the first occurance of text
foreach ($arrays as $key => $array) {

        if(isset($tmp_list[$array['text']])){
            //possible match check for time difference
            $difference = $tmp_list[$array['text']]->diff($array['time']);

            if($difference->y === 0 && $difference->m === 0 && $difference->d === 0 && $difference->h === 0 && $difference->i <= 59 && $difference->s <= 59){
                //less than hour difference
            }else{
                //more than hour difference
            }


        }else{
            $tmp_list[$array['text']] = $array['time'];
        }   

}
}

findMatch($arrays);
函数findMatch($arrays){
$tmp_list=[];//取第一个出现的文本
foreach($key=>$array的数组){
如果(isset($tmp_列表[$array['text']])){
//可能的时差匹配检查
$difference=$tmp_列表[$array['text']]->diff($array['time']);

如果($difference->y===0&&$difference->m==0&&$difference->d==0&&$difference->h==0&&$difference->i s尝试使用给定数组执行此操作 我对时差的解决方案可能不太好,但我注意到另一个答案并不会在时差中花费天/月/年,因此如果日期不同,但一天中的时间小于60分钟,它就会过去

无论第一个日期是更老还是更年轻,这种方法都有效,因此不需要数组排序

function findMatch($arrays){
$tmp_list = []; //takes the first occurance of text
foreach ($arrays as $key => $array) {

        if(isset($tmp_list[$array['text']])){
            //possible match check for time difference
            $difference = $tmp_list[$array['text']]->diff($array['time']);

            if($difference->y === 0 && $difference->m === 0 && $difference->d === 0 && $difference->h === 0 && $difference->i <= 59 && $difference->s <= 59){
                //less than hour difference
            }else{
                //more than hour difference
            }


        }else{
            $tmp_list[$array['text']] = $array['time'];
        }   

}
}

findMatch($arrays);
函数findMatch($arrays){
$tmp_list=[];//取第一个出现的文本
foreach($key=>$array的数组){
如果(isset($tmp_列表[$array['text']])){
//可能的时差匹配检查
$difference=$tmp_列表[$array['text']]->diff($array['time']);

如果($difference->y===0&&$difference->m==0&&$difference->d==0&&$difference->h==0&&$difference->i s,你可以说“你好”,“另一个文本”,“你好”这些记录是按时间顺序或随机的还是按文本顺序排列的?@ LeloFayeta可以有不同的文本字符串,但只考虑一个小时内有完全相同的字符串和日期的匹配,而不是回答我的问题……我问输入的结构是什么,而不是输出应该是什么样子,我会把这些分组。以文本开头(将其用作关联数组的键),并将该文本的所有日期放入该键下的一个数组中。读取所有数据后,对这些日期进行排序,然后检查它们之间的差异…@Zabs这确实有帮助。您首先必须按文本排序,然后按日期排序,保持文本先排序。然后您可以看到eac之间的时差你有“你好”,“另一条短信”,“你好”这些记录是按时间顺序或随机的还是按文本顺序排列的?@ LeloFayeta可以有各种文本串,但只考虑一个小时内有完全相同的字符串和日期的匹配,而不是回答我的问题……我问输入的结构是什么,而不是输出应该如何看起来像我将分组那些。