到目前为止我得到了什么:
$dates[] = array("date" => "2016-02-18 02:00:00", "duration" => "600");
$dates[] = array("date" => "2016-02-18 02:05:00", "duration" => "300");
$dates[] = array("date" => "2016-02-18 02:10:00", "duration" => "600");
$dates[] = array("date" => "2016-02-18 02:25:00", "duration" => "300");
$dates[] = array("date" => "2016-02-18 02:30:00", "duration" => "600");
$pickedDates = [];
function findMeetingAlreadyExistsInArray($array, $dateTime){
foreach($array as $item){
if ($item["date"] === $dateTime) return true;
}
}
function closestDate($dateArray, $compareDate, &$pickedDates = array()){
$dates = [];
foreach($dateArray as $key => $originalDate){
$date = $originalDate['date'];
if(!findMeetingAlreadyExistsInArray($pickedDates, $date)){
if(abs(strtotime($compareDate["date"]) - strtotime($date)) >= $compareDate["duration"]){
$dates[$key] = abs(strtotime($compareDate["date"]) - strtotime($date));
}
}
}
asort($dates);
$dateIndex = array_shift(array_keys($dates));
array_push($pickedDates, $dateArray[$dateIndex]);
return $dateArray[$dateIndex];
}
foreach($dates as $item) closestDate($dates, $item, $pickedDates);
print_r($pickedDates);
以上功能的结果:
Array
(
[0] => Array
(
[date] => 2016-02-18 02:10:00
[duration] => 600
)
[1] => Array
(
[date] => 2016-02-18 02:00:00
[duration] => 600
)
[2] => Array
(
[date] => 2016-02-18 02:25:00
[duration] => 300
)
[3] => Array
(
[date] => 2016-02-18 02:30:00
[duration] => 600
)
[4] => Array
(
[date] => 2016-02-18 02:05:00
[duration] => 300
)
)
预期结果:
Array
(
[0] => Array
(
[date] => 2016-02-18 02:00:00
[duration] => 600
)
[1] => Array
(
[date] => 2016-02-18 02:10:00
[duration] => 600
)
[2] => Array
(
[date] => 2016-02-18 02:25:00
[duration] => 300
)
[3] => Array
(
[date] => 2016-02-18 02:30:00
[duration] => 600
)
)
答案 0 :(得分:1)
这是我的尝试......我改变了很多,只是为了完全按照你想做的事情来解决。
$dates[] = array("date" => "2016-02-18 02:00:00", "duration" => "600"); // 10 mins
$dates[] = array("date" => "2016-02-18 02:05:00", "duration" => "300"); // 5 mins
$dates[] = array("date" => "2016-02-18 02:10:00", "duration" => "600");
$dates[] = array("date" => "2016-02-18 02:25:00", "duration" => "300");
$dates[] = array("date" => "2016-02-18 02:30:00", "duration" => "600");
$pickedDates = [];
function dateExists(array $array, $date) {
if (empty($array)) {
return false;
}
$flag = false;
foreach($array as $value) {
// exact match
if ($value['date'] == $date) {
return true;
}
// range match
$start = strtotime($value['date']);
$end = $start + $value['duration'];
$ts = strtotime($date);
if ($ts > $start && $ts < $end) {
$flag = true;
break;
}
}
return $flag;
}
foreach ($dates as $key => $date) {
if (!dateExists($pickedDates, $date['date'])) {
$pickedDates[$key] = $date;
}
}
echo '<pre>';
var_dump($pickedDates);
echo '</pre>';
这会产生预期的结果:
array(4) {
[0]=>
array(2) {
["date"]=>
string(19) "2016-02-18 02:00:00"
["duration"]=>
string(3) "600"
}
[2]=>
array(2) {
["date"]=>
string(19) "2016-02-18 02:10:00"
["duration"]=>
string(3) "600"
}
[3]=>
array(2) {
["date"]=>
string(19) "2016-02-18 02:25:00"
["duration"]=>
string(3) "300"
}
[4]=>
array(2) {
["date"]=>
string(19) "2016-02-18 02:30:00"
["duration"]=>
string(3) "600"
}
}
不确定您的closestDate
函数是否必须存在并且具有这些确切的参数。无论哪种方式,您都应该能够修改它以满足您的要求。