使用相同键的检查点创建多维PHP数组

时间:2020-09-06 06:04:38

标签: php arrays laravel-6

我正在尝试使用foreach循环在PHP中创建多维数组。我有一个用于存储日期的天表和一个用于存储开放和关闭时间以及该天的参考ID的小时表。每天都有两班倒。这是到目前为止我尝试过的代码:

foreach ($days as $value) {
            foreach ($value->hours as $key=>$val) {
                if (array_key_exists($value->name, $storeSchedule)) {
                    $storeSchedule[$value->name][] = array($val->opening_time => $val->closing_time);
                }else{
                    $storeSchedule[$value->name][] = array($val->opening_time => $val->closing_time);
                }
            }
        }

当前输出:

Array
(
    [Mon] => Array
        (
            [0] => Array
                (
                    [11:00:00] => 02:00:00
                )
            [1] => Array
                (
                    [16:00:00] => 22:00:00
                )
        )
    [Tue] => Array
        (
            [0] => Array
                (
                    [11:00:00] => 02:00:00
                )
            [1] => Array
                (
                    [16:00:00] => 22:00:00
                )
        )
    [Wed] => Array
        (
            [0] => Array
                (
                    [11:00:00] => 02:00:00
                )
            [1] => Array
                (
                    [16:00:00] => 22:00:00
                )
        )
    [Thur] => Array
        (
            [0] => Array
                (
                    [11:00:00] => 02:00:00
                )
            [1] => Array
                (
                    [16:00:00] => 22:00:00
                )
        )
    [Fri] => Array
        (
            [0] => Array
                (
                    [11:00:00] => 02:00:00
                )
            [1] => Array
                (
                    [16:00:00] => 22:00:00
                )
        )
    [Sat] => Array
        (
            [0] => Array
                (
                    [11:30:00] => 15:30:00
                )
            [1] => Array
                (
                    [16:00:00] => 21:00:00
                )
        )
    [Sun] => Array
        (
            [0] => Array
                (
                    [11:30:00] => 15:30:00
                )
            [1] => Array
                (
                    [16:00:00] => 21:00:00
                )
        )
)

预期输出:

Array
(
    [Sun] => Array
        (
            [11:30 AM] => 03:30 PM
            [04:00 PM] => 09:00 PM
        )
    [Mon] => Array
        (
            [11:00 AM] => 2:00 PM
            [04:00 PM] => 10:00 PM
        )
    [Tue] => Array
        (
            [11:00 AM] => 2:00 PM
            [04:00 PM] => 10:00 PM
        )
    [Wed] => Array
        (
            [11:00 AM] => 2:00 PM
            [04:00 PM] => 10:00 PM
        )
    [Thu] => Array
        (
            [11:00 AM] => 2:00 PM
            [04:00 PM] => 10:00 PM
        )
    [Fri] => Array
        (
            [11:00 AM] => 2:00 PM
            [04:00 PM] => 10:00 PM
        )
    [Sat] => Array
        (
            [11:30 AM] => 03:30 PM
            [04:00 PM] => 09:00 PM
        )
)

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。请在下面找到更正的foreach循环:

foreach ($days as $value) {
            foreach ($value->hours as $key=>$val) {
                if (array_key_exists($value->name, $storeSchedule)) {
                    foreach ($storeSchedule[$value->name] as $k => $v) {
                        $ee=array($k => $v, $val->opening_time => $val->closing_time);
                    }
                    unset($storeSchedule[$value->name]);
                    $storeSchedule[$value->name] = $ee;
                }else{
                    $storeSchedule[$value->name] = array($val->opening_time => $val->closing_time);
                }
            }
        }