检查重复的值并将它们合并为php-mysql



我如何知道重复的值,并删除它,

这是我的密码。。。

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

}

这是我的结果,

{
"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"
}

若max_start,max_end已经存在,那个么取最大值。例如在max_start 06:30:00和max_end 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_,那是06:30:00到07:30:00。

请帮忙。

更新

当我向数组添加更多值时,第一个数组显示错误。正在显示,

2020-07-02 05:30:00 - 2020-07-02 07:30:00 
2020-07-02 07:30:00 - 2020-07-02 08:30:00..... 

但是,它应该显示,

2020-07-02 05:30:00 - 2020-07-02 06:30:00 
2020-07-02 06:30:00 - 2020-07-02 07:30:00.....

这是我的代码

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

这是我的结果:

[
[
"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"
]
]

上一个代码:

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

这里有一个片段,可以使用您发布的数据格式。

$initialData = $data = [
[
'max_start' => '2020-07-02 05:30:00',
'max_end' => '2020-07-02 06:30:00',
],
[
'max_start' => '2020-07-02 07: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',
]
];
// 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 start at the same time
if ($interval['max_start'] == $currentInterval['max_start']) {
// 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;
}
echo 'Initial list: ', PHP_EOL, print_r($initialData, true);
echo 'Merged list: ', PHP_EOL, print_r($result, true);

此代码段具有以下输出:

Initial list: 
Array
(
[0] => Array
(
[max_start] => 2020-07-02 05:30:00
[max_end] => 2020-07-02 06:30:00
)
[1] => Array
(
[max_start] => 2020-07-02 07:00:00
[max_end] => 2020-07-02 07:30:00
)
[2] => Array
(
[max_start] => 2020-07-02 06:30:00
[max_end] => 2020-07-02 07:00:00
)
[3] => Array
(
[max_start] => 2020-07-02 06:30:00
[max_end] => 2020-07-02 07:30:00
)
)
Merged list: 
Array
(
[0] => Array
(
[max_start] => 2020-07-02 05:30:00
[max_end] => 2020-07-02 06:30:00
)
[1] => Array
(
[max_start] => 2020-07-02 06:30:00
[max_end] => 2020-07-02 07:30:00
)
[2] => Array
(
[max_start] => 2020-07-02 07:00:00
[max_end] => 2020-07-02 07:30:00
)
)

如果符合您的需求或需要进一步调整,请告诉我。


对于7.0之前的PHP版本,将usort代码替换为以下代码:

usort($data, function($a, $b){
if ($a['max_start'] == $b['max_start']) {
return 0;
}
return $a['max_start'] > $b['max_start'] ? -1 : 1;
});

请注意,PHP 5.6于2018年12月31日达到寿命终止状态,不建议再使用它。

相关内容

  • 没有找到相关文章

最新更新