根据价值加入一周中的几天

时间:2016-03-22 22:16:33

标签: php arrays

我有一个表单,允许用户输入他们从周一到周日工作的时间。

例如,用户可以输入以下内容:

Monday: 8am to 5pm
Tuesday: 8am to 5pm
Weds: 10am to 12pm
Thursday: 8am to 5pm
Friday: 8am to 5pm
Saturday: None
Sunday: None

我将这些数据放在数组$office中,将工作日作为工作时间的关键。

现在我正在尝试将相互关注的日子合并到相同的工作时间,因此我可以输出如下数据:

Monday - Tuesday: 8am to 5pm
Weds: 10am to 12pm
Thursday - Friday: 8am to 5pm
Saturday - Sunday: None

现在我正在尝试使用很多大的if / else语句来执行此操作,如果它们具有相同的工作时间,我会检查每个组合的日期。

它的一小部分片段,让你知道我尝试了什么:

<?php

    $monday = $office['monday'];
    $tuesday = $office['tuesday'];
    $wednesday = $office['wednesday'];
    $thursday = $office['thursday'];
    $friday = $office['friday'];
    $saturday = $office['saturday'];
    $sunday = $office['sunday'];


    if($monday == $tuesday && $monday == $wednesday && $monday == $thursday && $monday == $friday && $monday == $saturday && $monday == $sunday){
        echo "<li><strong>Monday - Sunday</strong>: {$monday}</li>";
    } elseif($monday == $tuesday && $monday == $wednesday && $monday == $thursday && $monday == $friday && $monday == $saturday){
        echo "<li><strong>Monday - Saturday</strong>: {$monday}</li>";
        echo "<li><strong>Sunday</strong>: {$sunday}</li>";
    }

    //... All combinations of days with the same work hours

    } else{
        echo "<li><strong>Monday</strong>: {$monday}</li>";
        echo "<li><strong>Tuesday</strong>: {$tuesday}</li>";
        echo "<li><strong>Wednesday</strong>: {$wednesday}</li>";
        echo "<li><strong>Thursday</strong>: {$thursday}</li>";
        echo "<li><strong>Friday</strong>: {$friday}</li>";
        echo "<li><strong>Saturday</strong>: {$saturday}</li>";
        echo "<li><strong>Sunday</strong>: {$sunday}</li>";
    }

现在我希望并想知道是否有更简单/更清洁的方法来做到这一点?也许是一个PHP函数或者比我现在更小更干净的代码?

0 个答案:

没有答案