How can i know duplicate values, and remove that,
Here is my code...
function _get($limit=NULL)
{
$results = array();
$single = array();
$this->db->select("MAX(schedule.start_at) as max_start, MAX(schedule.end_at) as max_end");
$this->db->group_by(array("schedule.start_at", "schedule.end_at"));
$this->db->from("schedule2_tbl as schedule");
$q = $this->db->get();
if( $q->num_rows() )
{
$data = $q->result_array();
// return $data;
foreach($data as $key => $row){
$single[] = $row;
}
$results = $single;
return $results;
}
}
This is my result,
{
"max_start": "2020-07-02 05:30:00",
"max_end": "2020-07-02 06:30:00"
},
{
"max_start": "2020-07-02 06:30:00",
"max_end": "2020-07-02 07:00:00"
},
{
"max_start": "2020-07-02 06:30:00",
"max_end": "2020-07-02 07:30:00"
},
{
"max_start": "2020-07-02 07:00:00",
"max_end": "2020-07-02 07:30:00"
}
If max_start, max_end is already there, then take greatest value. For example, in case of max_start 06:30:00 and max_end 07:30:00, There is three values,
{
"max_start": "2020-07-02 06:30:00",
"max_end": "2020-07-02 07:00:00"
},
{
"max_start": "2020-07-02 06:30:00",
"max_end": "2020-07-02 07:30:00"
},
{
"max_start": "2020-07-02 07:00:00",
"max_end": "2020-07-02 07:30:00"
}
In this case i have to store only most smallest value of max_start and greatest max_end value, That's 06:30:00 to 07:30:00.
Please help.
UPDATE
When i adding more values to the array,firs array is showing wrongly. It's showing,
2020-07-02 05:30:00 - 2020-07-02 07:30:00
2020-07-02 07:30:00 - 2020-07-02 08:30:00.....
But,It should show,
2020-07-02 05:30:00 - 2020-07-02 06:30:00
2020-07-02 06:30:00 - 2020-07-02 07:30:00.....
Here is my code
$initialData = $data = [
[
"max_start": "2020-07-02 05:30:00",
"max_end": "2020-07-02 06:30:00"
],
[
"max_start": "2020-07-02 06:00:00",
"max_end": "2020-07-02 07:30:00"
],
[
"max_start": "2020-07-02 06:30:00",
"max_end": "2020-07-02 07:00:00"
],
[
"max_start": "2020-07-02 06:30:00",
"max_end": "2020-07-02 07:30:00"
],
[
"max_start": "2020-07-02 07:00:00",
"max_end": "2020-07-02 07:30:00"
],
[
"max_start": "2020-07-02 07:30:00",
"max_end": "2020-07-02 08:30:00"
],
[
"max_start": "2020-07-02 08:30:00",
"max_end": "2020-07-02 09:30:00"
],
[
"max_start": "2020-07-02 09:30:00",
"max_end": "2020-07-02 11:15:00"
],
[
"max_start": "2020-07-02 11:15:00",
"max_end": "2020-07-02 11:30:00"
],
[
"max_start": "2020-07-02 11:30:00",
"max_end": "2020-07-02 12:00:00"
],
[
"max_start": "2020-07-02 11:30:00",
"max_end": "2020-07-02 12:30:00"
],
[
"max_start": "2020-07-02 12:00:00",
"max_end": "2020-07-02 12:30:00"
],
[
"max_start": "2020-07-02 12:30:00",
"max_end": "2020-07-02 13:00:00"
],
[
"max_start": "2020-07-02 12:30:00",
"max_end": "2020-07-02 13:30:00"
],
[
"max_start": "2020-07-02 13:00:00",
"max_end": "2020-07-02 13:30:00"
]
];
// Order the list chronologically by the "max_start" value, to make comparison easier later
usort($data, function($a, $b){
return $a['max_start'] <=> $b['max_start'];
});
// Final result will be collected here
$result = [];
// Work with the first list value as long there is one
while ($currentInterval = array_shift($data)) {
// Compare with each other value in the list
foreach ($data as $index => $interval) {
// Check if intervals overlap
// Replace "<" with a "<=" if you want to merge intervals that "touch": one interval ends at the same time another one begins
if ($interval['max_start'] < $currentInterval['max_end']) {
// Merge when needed
$currentInterval['max_end'] = max ($currentInterval['max_end'], $interval['max_end']);
// Remove the merged interval
unset($data[$index]);
}
}
// Add to result
$result[] = $currentInterval;
}
return $result;
Here is my result:
[
[
"max_start": "2020-07-02 05:30:00",
"max_end": "2020-07-02 07:30:00"
],
[
"max_start": "2020-07-02 07:30:00",
"max_end": "2020-07-02 08:30:00"
],
[
"max_start": "2020-07-02 08:30:00",
"max_end": "2020-07-02 09:30:00"
],
[
"max_start": "2020-07-02 09:30:00",
"max_end": "2020-07-02 11:15:00"
],
[
"max_start": "2020-07-02 11:15:00",
"max_end": "2020-07-02 11:30:00"
],
[
"max_start": "2020-07-02 11:30:00",
"max_end": "2020-07-02 12:30:00"
],
[
"max_start": "2020-07-02 12:30:00",
"max_end": "2020-07-02 13:30:00"
]
]
Previous code:
$initialData = $data = $q->result_array();
// Order the list chronologically by the "max_start" value, to make comparison easier later
usort($data, function($a, $b){
return $a['max_start'] <=> $b['max_start'];
});
// Final result will be collected here
$result = [];
// Work with the first list value as long there is one
while ($currentInterval = array_shift($data)) {
// Compare with each other value in the list
foreach ($data as $index => $interval) {
// Check if intervals overlap
// Replace "<" with a "<=" if you want to merge intervals that "touch": one interval ends at the same time another one begins
if ($interval['max_start'] < $currentInterval['max_end']) {
// Merge when needed
$currentInterval['max_end'] = max ($currentInterval['max_end'], $interval['max_end']);
// Remove the merged interval
unset($data[$index]);
}
}
// Add to result
$result[] = $currentInterval;
}
return $result;