工作日的小时数组-尝试将相同的日子组合在一起



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

很容易,如果我只是做每一天跟着它的时间,但是如果星期一到星期五都是一样的,我想显示它为星期一到星期五!

这是我的数组(命名为$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变量没有像我期望的那样重置。

试试这个:

//$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

它只在一天只有一个开放/关闭时间(数组)时起作用。如果任何地方每天打开/关闭两次(或更多次),你需要稍微改变一下代码。

查看操作

的代码

最新更新