Reputation: 1448
I'll clarify my question.
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"
}`
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.
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
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).
Upvotes: 2
Views: 80
Reputation: 1691
Try this:
$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);
Upvotes: 1
Reputation: 106
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;
}
Upvotes: 2