如何从数组中找到最近的日期,以推入已经选择的日期数组 [英] How to find the closest date from array to push into already chosen dates array

查看:132
本文介绍了如何从数组中找到最近的日期,以推入已经选择的日期数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$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");
function closestDates($array, $date){   
    foreach($array as $day)
         $interval[] = abs(strtotime($date["date"]) - strtotime($day["date"]));
    asort($interval);
    $closest = key($interval);
    $alreadyChosen[] = $array[$closest];
    return $alreadyChosen;
}
$returnedDates = closestDates($dates, array("date" => "2016-02-18 02:00:00", "duration" => "600"));
print_r($returnedDates);

// This returns 
Array ( 
    [0] => Array ( 
        [date] => 2016-02-18 02:00:00 
        [duration] => 600 
    ) 
)

如何调整上述功能,以便通过日期数组查看,并将下一个最近的日期推送到 alreadyChosen array基于持续时间。

How can I adapt the above function to be able to check through dates array and push the next closest date to alreadyChosen array based on the duration time.

$dates = (
    '0'=> array("date" => "2016-02-18 02:00:00", "duration" => "600"),
    '1'=> array("date" => "2016-02-18 02:05:00", "duration" => "300"),
    '2'=> array("date" => "2016-02-18 02:10:00", "duration" => "600"),
    '3'=> array("date" => "2016-02-18 02:25:00", "duration" => "300"),
    '4'=> array("date" => "2016-02-18 02:30:00", "duration" => "600")
);

// Expected result to be after the checks:
$alreadyChosen = 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")
);


推荐答案

通过执行以下操作找到解决方案: (我从我的问题改变了很多事情,使其工作)

I've found a solution by doing the following: (I changed alot of things from my question to get it working)

$dates[] = array("date" => "2016-02-18 02:00:00", "meeting_id" => "1", "class_id" => "10", "duration" => "600");
$dates[] = array("date" => "2016-02-18 02:10:00", "meeting_id" => "1", "class_id" => "10", "duration" => "600");
$dates[] = array("date" => "2016-02-18 02:20:00", "meeting_id" => "1", "class_id" => "10", "duration" => "600");
$dates[] = array("date" => "2016-02-18 02:30:00", "meeting_id" => "1", "class_id" => "10", "duration" => "600");
$dates[] = array("date" => "2016-02-18 02:10:00", "meeting_id" => "2", "class_id" => "10", "duration" => "600");
$dates[] = array("date" => "2016-02-18 02:25:00", "meeting_id" => "2", "class_id" => "10", "duration" => "300");
$dates[] = array("date" => "2016-02-18 02:30:00", "meeting_id" => "3", "class_id" => "10", "duration" => "600");
$dates[] = array("date" => "2016-02-18 02:40:00", "meeting_id" => "3", "class_id" => "10", "duration" => "600");
$dates[] = array("date" => "2016-02-18 02:30:00", "meeting_id" => "4", "class_id" => "11", "duration" => "600");
$dates[] = array("date" => "2016-02-18 02:40:00", "meeting_id" => "4", "class_id" => "11", "duration" => "600");
$dates[] = array("date" => "2016-02-18 02:50:00", "meeting_id" => "4", "class_id" => "11", "duration" => "600");
$dates[] = array("date" => "2016-02-18 03:00:00", "meeting_id" => "4", "class_id" => "11", "duration" => "600");

$firstDates[] = array("date" => "2016-02-18 02:00:00", "meeting_id" => "1", "class_id" => "10", "duration" => "600");
$firstDates[] = array("date" => "2016-02-18 02:10:00", "meeting_id" => "1", "class_id" => "10", "duration" => "600");
$firstDates[] = array("date" => "2016-02-18 02:20:00", "meeting_id" => "1", "class_id" => "10", "duration" => "600");
$firstDates[] = array("date" => "2016-02-18 02:30:00", "meeting_id" => "1", "class_id" => "10", "duration" => "600");

$children[] = array("id" => 1, "class_id" => "10", "fullname" => "Callum");
$children[] = array("id" => 2, "class_id" => "10", "fullname" => "Daniel");
$children[] = array("id" => 3, "class_id" => "11", "fullname" => "Jake");

function dateExists($array, $child, $date) {
    if (empty($array)) {
        return false;
    }
    $flag = false;
    foreach($array as $value) {
        if ($value['child']['id'] == $child['id'] && $value['meeting']['meeting_id'] === $date['meeting_id']){
            return true;
        }
        if ($value['meeting']['date'] == $date['date']) {
            return true;
        }
        $start = strtotime($value['meeting']['date']);
        $end = $start + $value['meeting']['duration'];
        $ts = strtotime($date['date']);
        if ($ts > $start && $ts < $end) {
            $flag = true;
            break;
        }
    }
    return $flag;
}


$results = [];

function buildDates($dates, $children, $key, $firstDate) {
    $temp = [];
    $pickedDates = array();
    foreach ($children as $child) {
        if($firstDate["class_id"] === $child["class_id"]) {
            $temp[] = array('child' => $child, 'meeting' => $firstDate);
            $pickedDates[$firstDate['date']] = $temp;
        }
        break;
    }
    foreach ($dates as $key => $date) {
        foreach ($children as $child) {
            if($date["class_id"] === $child["class_id"]) {
                if (!dateExists($temp, $child, $date)) {
                    $temp[] = array('child' => $child, 'meeting' => $date);
                    $pickedDates[$firstDate['date']] = $temp;
                }
            }
        }
    }
    return $pickedDates = array($firstDate["date"] => $temp);
}

foreach ($firstDates as $date) {
    $results[] = buildDates($dates, $children, $date);
}

print_r($results);

这篇关于如何从数组中找到最近的日期,以推入已经选择的日期数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆