Php 工作日的小时数数组-尝试合并相同的天

Php 工作日的小时数数组-尝试合并相同的天,php,Php,通过数据库控制网站的运行时间,这样最终用户就可以摆弄它们,我正试图以一种整洁的方式显示它们 如果我只做每天的工作,然后是工作时间,那就很容易了,但是如果周一到周五都一样,我想把它显示为周一到周五 这是我的数组(称之为$temp) 有些地方一天会开/关两次,因此时间在另一个阵列中 Array ( [Monday] => Array ( [0] => Array ( [

通过数据库控制网站的运行时间,这样最终用户就可以摆弄它们,我正试图以一种整洁的方式显示它们

如果我只做每天的工作,然后是工作时间,那就很容易了,但是如果周一到周五都一样,我想把它显示为周一到周五

这是我的数组(称之为
$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
仅当一天只有一个开放/关闭小时(数组)时,它才起作用。如果任何地方每天打开/关闭两次(或更多),您需要稍微更改代码