根据Day&在搜索数组中找到最佳匹配项时间 [英] Search Array for Best Match based on Day & Time

查看:47
本文介绍了根据Day&在搜索数组中找到最佳匹配项时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据数组中的当前日期和时间来确定正确的网站。示例数组仅显示星期一,实际数组将包含一周中的7天,每天有多个值。

I'm trying to work out how to the the correct Site base on the current Day and Time from an array. The examples array just shows Monday, the real array will contain 7 days of the week with multiple values for each day.

这是示例数组:

$arr = array (
    array(  'Day' => 'Monday',
            'Start' => '0830',
            'End' => '1730',
            'Site' => 'NW1'),

    array(  'Day' => 'Monday',
            'Start' => '1200',
            'End' => '1300',
            'Site' => 'PL1'),

    array(  'Day' => 'Monday',
            'Start' =>'1730',
            'End' => '2130',
            'Site' => 'RE1')
);

所以星期一在 1100 我应该得到 NW1 ,在 1800 ,我应该得到 RE1 ,但在 1200 - 1300 我应该得到 PL1

So Monday at 1100 I should get NW1, at 1800 I should get RE1, but between 1200-1300 I should get PL1

到目前为止,这是我的代码:

So far this is the code I have:

$today = 'Monday'; // Full day name
$time = '1205';

echo "<br/>Day: $today";
echo "<br/>Time: $time <br/><br/>";


$arr = array (
    array(  'Day' => 'Monday',
            'Start' => '0830',
            'End' => '1730',
            'Site' => 'NW1'),

    array(  'Day' => 'Monday',
            'Start' => '1200',
            'End' => '1300',
            'Site' => 'PL1'),

    array(  'Day' => 'Monday',
            'Start' =>'1730',
            'End' => '2130',
            'Site' => 'RE1')
);

usort($arr, function($a, $b) {
    return (date("N", strtotime($a['Day'])) <=> date("N", strtotime($b['Day']))) * 100 +
           ($a['Start'] <=> $b['Start']) * 10 +
           ($a['End'] <=> $b['End']);
});

foreach ($arr as $i => $values) {
    if ($today != $values['Day']) continue;

    if ($time >= $values['Start'] && $time <= $values['End']) {
        $site = $values['Site'];
        break;
    }
}

echo "$today @ $time Site => $site";

此功能适用于 0830-1730 & 1730-2130 ,但是如果时间是 1200-1300 ,则不会。

This works for times between 0830-1730 & 1730-2130, but not if the time is 1200-1300.

我假设我需要在数组中搜索天匹配项,然后搜索开始时间并检查结束时间,但是我不确定如何执行此操作?

I'm assuming I need to search the array for the Day match, then the Start time and check the end time, but I'm not sure how to do this ?

有人能指出我正确的方向吗。

Can anyone point me in the right direction.

谢谢

**** UPDATE ****
带有附加条目的新示例数组

**** UPDATE **** New example array with additional entries

Array
(
    [0] => Array
        (
            [Day] => Monday
            [Start] => 0830
            [End] => 1730
            [Site] => NW1
        )

    [1] => Array
        (
            [Day] => Monday
            [Start] => 0930
            [End] => 0945
            [Site] => PK1
        )

    [2] => Array
        (
            [Day] => Monday
            [Start] => 1200
            [End] => 2100
            [Site] => PL1
        )

    [3] => Array
        (
            [Day] => Monday
            [Start] => 1230
            [End] => 1245
            [Site] => EM1
        )

    [4] => Array
        (
            [Day] => Monday
            [Start] => 1730
            [End] => 2130
            [Site] => RE1
        )
}

预期结果为:

0940 = PK1
1430 = PL1
0920 = NW1

目标是0830至1730 NW1是正确的,除非有其他方法可以改写,即1200-2100 PL1在2100至2130 RE1之后是正确的,等等。

The aim is 0830 to 1730 NW1 is correct unless something else overrides this, ie 1200-2100 PL1 would be correct, after 2100 to 2130 RE1 etc.

谢谢

推荐答案

所有您需要做的就是根据开始时间反向排序

All you need to do is reverse the sorting based on start time

usort($arr, function($a, $b) {
    return (date("N", strtotime($a['Day'])) <=> date("N", strtotime($b['Day']))) * 100 +
           ($b['Start'] <=> $a['Start']) * 10 +
           ($a['End'] <=> $b['End']);
});

这样,我们将从最新的开始时间(每天)开始遍历数组,然后一旦找到匹配项,我们就会使用匹配项的站点值来打破循环。

This way, we will loop through the array starting with the latest start time (for each day) and once we find a match, we break the loop, using the site value for the match.

我仍然不太确定您 usort ,但它似乎没有引起任何问题,因此我将其保留。

I'm still not really sure of the purpose of the multiplication in your usort, but it doesn't seem to be causing any problems, so I'm going to leave it in.

演示

这篇关于根据Day&amp;在搜索数组中找到最佳匹配项时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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