工作日的小时数 - 试图结合相同的日子

时间:2014-07-24 14:46:17

标签: php

从数据库控制网站上的操作时间,以便最终用户可以摆弄它们,并且我试图以一种整洁的方式显示它们。

如果我每天只做几个小时就足够了,但是如果周一周五都是一样的话,我想把它显示为星期一 - 星期五!

这是我的数组(称之为$temp

一些地方将在一天内打开/关闭两次,因此小时数在另一个阵列中。

Array
(
    [Monday] => Array
        (
            [0] => Array
                (
                    [open] => 08:00am
                    [close] => 08:30pm
                )

        )

    [Tuesday] => Array
        (
            [0] => Array
                (
                    [open] => 08:00am
                    [close] => 08:30pm
                )

        )

    [Wednesday] => Array
        (
            [0] => Array
                (
                    [open] => 08:00am
                    [close] => 08:30pm
                )

        )

    [Thursday] => Array
        (
            [0] => Array
                (
                    [open] => 08:00am
                    [close] => 08:30pm
                )

        )

    [Friday] => Array
        (
            [0] => Array
                (
                    [open] => 08:00am
                    [close] => 08:30pm
                )

        )

    [Saturday] => Array
        (
            [0] => Array
                (
                    [open] => 10:30am
                    [close] => 08:30pm
                )

        )

    [Sunday] => Array
        (
            [0] => Array
                (
                    [open] => 10:30am
                    [close] => 08:30pm
                )

        )

)

以下是我拼凑的代码,试图将上述数组操作为相同的分组天数:

$start = current(array_keys($temp)); //The first open weekday
$end = end(array_keys($temp)); //(SHOULD) only makes the end day the very last day IF they are all the same hours. will reset later.
$last = null;
$count = 0;
foreach($temp as $day => $hours) {
    if($hours == $last) {
        $count++;
        $end = $day; // advance the day thats going to be the end.
        continue; // dont want to hit the resets at the bottom.
    }
    else { 
        if($count = 0) {
            $return['reg_hours'][$yesterday] = $last; //yesterdays hours had no matches so set it alone.
        }
        else { //There is a string of days with the same hours, so concat them
            $return['reg_hours'][$start . ' - ' . $end] = $hours; // i.e. $return['Monday - Thursday'] =  '07:00am - 10:30am'
        }   
        $start = $day; //a new string of hours is starting so reset the start to today.
    }
    $count = 0;
    $yesterday = $day;
    $last = $hours;
}

我在思考过程中发表了评论。如果每天都有相同的时间,这种方法可以正常工作,但如果小时数完全不同,则会出现明显的意外结果。下面是它返回上面的数组:

Monday - Sunday 08:00am - 08:30pm
Monday - Friday 10:30am - 08:30pm

预期结果:

Monday - Friday 08:00am - 08:30pm
Saturday - Sunday 10:30am - 08:30pm

无法弄清楚为什么$start$end变量不会像我期望的那样重置。任何指导都表示赞赏。

1 个答案:

答案 0 :(得分:0)

试试这个:

//$days are the array with the data

$days_new = array();

$current_day = current($days)[0];
$days_new[] = array(
                    "init" => key($days),
                    "end" => key($days),
                    "open" => $current_day['open'],
                    "close" => $current_day['close']
                );

foreach($days as $key => $day){
    //update end day
    if( $day[0]['open'] == $current_day['open'] 
        && $day[0]['close'] == $current_day['close'])
    {
        $days_new[count($days_new) - 1]['end'] = $key;
    }
    //create new reg
    else{
        $days_new[] = array(
                            "init" => $key,
                            "end" => $key,
                            "open" => $current_day['open'],
                            "close" => $current_day['close']
                    );
    }
    $current_day = $day[0];
}

打印您需要执行以下操作的结果:

foreach($days_new as $key => $item){
    print $item['init'];
    $item['init'] == $item['end'] ? print " " : print " - " . $item['end'] . " ";
    print $item['open'] . " - " . $item['close'] . "\n";
}

结果是:

Monday - Friday 08:00am - 08:30pm
Saturday - Sunday 08:00am - 08:30pm

只有当一天只有一个打开/关闭小时(数组)时,它才有效。如果任何地方每天(或更多)打开/关闭两次,您需要稍微更改一下代码。

See the code on action