如果其日期(属性)相对于任何其他对象日期在15天内,则删除数组列表对象 [英] Remove Array list object if its date(property) is with in 15 days with respect to any other object date

查看:63
本文介绍了如果其日期(属性)相对于任何其他对象日期在15天内,则删除数组列表对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将澄清问题。

我有对象的数组列表,其中一个属性是日期。我想要一个对象列表,它们之间的间隔至少为15天。

I have array list of objects, where one of its property is date. I want a list of objects where their is a minimum gap of 15 days between each object in the array.

    {
        "id": 1,
        "date" : "21-04-2017",
        "report" : "Temp",
        "Test" : "7500000"
    }
    {
        "id": 2,
        "dateReported" : "29-03-2017",
        "report" : "Temp",
        "Test" : "7500000"
    }, 
    {
        "id": 3,
        "dateReported" : "29-03-2017",
        "report" : "Temp",
        "Test" : "7500000"
    }, 
    {
        "id": 4,
        "dateReported" : "23-03-2017",
        "report" : "Temp",
        "Test" : "7500000"
    }, 
    {
        "id": 5,
        "dateReported" : "02-02-2017",
        "report" : "Temp",
        "Test" : "7500000"
    }, 
    {
        "id": 6,
        "dateReported" : "01-02-2017",
        "report" : "Temp",
        "Test" : "7500000"
    }`

在上面的列表中,对象ID: 1,2,5必须是结果,因为例如对象ID:3在对象ID:2的15天之内,并且类似地,ID:4在对象ID:5的15天之内。

In the above list, Object id: 1, 2, 5 has to be the result because for example object id: 3 is within 15 days of object id: 2 and similarly id: 4 and object id:6 is within 15days of object id:5.

我编写了以下代码,其中我可以在循环中删除对象ID:3和6,但我也无法删除对象ID:4,该ID也在对象的15天内id:2

I have written following code, where I am able to remove object id: 3 and 6 within a loop but I am not able to remove object id:4 which is also within 15 days of object id:2

以下是我尝试过的php代码:

Following is the php code which I tried:

$finalFilteredList = [];
// Filter out array objects, if any array objects have dates with in 15 days.
for ($index = 0, $j = 0; $index < count($intermediateList); $index++) {
    $counter = $index + 1;
    if (isset($intermediateList[$counter])) {
        // Find the difference between counter date and index date.
        $diffTimePeriod = date_diff(date_create($intermediateList[$counter]['date']), date_create($intermediateList[$index]['date']));
        $diffInDays = $diffTimePeriod->format('%d') + (30 * $diffTimePeriod->format('%m')) + (360 * $diffTimePeriod->format('%y'));
        if ($diffInDays < 15) {
            // Considering as duplicate entry and hence ignoring.
        } else {
            $finalFilteredList[$j] = $intermediateList[$index];
            $j++;
        }
    }
}

我相信数组必须进行排序以使我的上述代码正常工作,并且我假设现在进行排序(以降低复杂性)。

I believe the array has to be sorted for my above code to work and I assuming that it is sorted for now(to reduce the complexity).

推荐答案

如果您的列表已经按日期排序,如下所示可能更容易理解:

If your list is already sorted by date, something like below could be more readable:

$sortedList = [...your sorted list...];
$filteredList = [];
$firstItem = array_shift($sortedList);
$filteredList[] = $firstItem;
$currentDate = DateTime::createFromFormat('d-m-Y', $firstItem['dateReported']);

foreach($sortedList as $item){

    $nextDate = DateTime::createFromFormat('d-m-Y', $item['dateReported']);
    $interval = $currentDate->diff($nextDate);

    if($interval->days < 15){ continue;}

    $currentDate = $nextDate;
    $filteredList[] = $item;

}

这篇关于如果其日期(属性)相对于任何其他对象日期在15天内,则删除数组列表对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆