如果其日期(属性)在15天内相对于任何其他对象日期,则删除数组列表对象

时间:2017-06-09 12:29:45

标签: php arrays

我会澄清我的问题。

我有对象的数组列表,其中一个属性是date。我想要一个对象列表,它们在数组中每个对象之间的最小间隔为15天。

    {
        "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:6在15天内对象id:5。

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

以下是我试过的php代码:

$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++;
        }
    }
}

我相信数组必须按照上面的代码进行排序才能工作,我假设它现在已经排序(以降低复杂性)。

2 个答案:

答案 0 :(得分:2)

如果您的列表已按日期排序,则下面的内容可能更具可读性:

$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;

}

答案 1 :(得分:1)

试试这个:

$jsonStr = '[{
        "id": "1",
        "dateReported" : "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"
    }]';
   $intermediateList = json_decode($jsonStr);
   $finalFilteredList = array();
   for ($index = 0, $j = 0; $index < count($intermediateList); $index++) {
       $counter = $index + 1;
       if (isset($intermediateList[$counter])) { 
            $datediff =  strtotime($intermediateList[$index]->dateReported) - strtotime($intermediateList[$counter]->dateReported);
            $datediffInDays = floor($datediff / (60 * 60 * 24));
            if($datediffInDays >= 15){ 
                if(empty($finalFilteredList)){
                    $finalFilteredList[] = $intermediateList[$index];
                    $finalFilteredList[] = $intermediateList[$counter];
                }else{
                    $finalFilteredList[] = $intermediateList[$counter];
                }
            }
       }
   }
echo "<pre>"; 
print_r($finalFilteredList); 

Check Code Snippet