0

我想要实现的是显示所有会话时间,并且在所有工作日中都有每个时间,然后每个工作日将显示一个或多个事件(如果有的话)。

我可以得到时间和工作日,但不能得到那些日子的事件。我已经提供了我的数组和代码,看看我能不能得到一些帮助。

我的会话时间

Array
(
[06:00:00] => 06:00:00
[07:00:00] => 07:00:00
[08:00:00] => 08:00:00
[09:00:00] => 09:00:00
[09:30:00] => 09:30:00
[17:30:00] => 17:30:00
[18:00:00] => 18:00:00
[18:30:00] => 18:30:00
[19:30:00] => 19:30:00
)

我的活动

Array
(
[0] => stdClass Object
(
[class_id] => 1
[class_name] => Fit Box
[class_description] => Fitbox is a high energy aerobic workout utilizing focus pads, kick pads, heavy bags, and speed balls. This class increases muscle strength and cardiovascular fitness and also includes strength and endurance circuit style training. Excellent for co-ordination, reflexes and to pump out the adrenalin! The class is 1 hour in duration.
[class_time] => 06:00:00
[class_day] => Tuesday
[class_status] => active
[class_colour] => blue
)

[1] => stdClass Object
(
[class_id] => 2
[class_name] => Hot Boxing
[class_description] => test description
[class_time] => 08:00:00
[class_day] => Wednesday
[class_status] => active
[class_colour] => grey
)

[2] => stdClass Object
(
[class_id] => 3
[class_name] => Punch Face
[class_description] => test again
[class_time] => 09:00:00
[class_day] => Thursday
[class_status] => active
[class_colour] => grey
)

[3] => stdClass Object
(
[class_id] => 4
[class_name] => MOS
[class_description] => test again
[class_time] => 19:30:00
[class_day] => Monday
[class_status] => active
[class_colour] => yellow
)

[4] => stdClass Object
(
[class_id] => 5
[class_name] => Yoga
[class_description] => test description
[class_time] => 08:00:00
[class_day] => Wednesday
[class_status] => active
[class_colour] => grey
)

)

这是我试图完成这项工作的代码。

    $result_array = array();
$days_array = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

foreach($timetable_times as $time_key => $time_value)
{
    // initialize all the days of the week for each time entry
    $result_array[$time_value['time']] = array();
    foreach($days_array as $day) {
        $result_array[$time_value['time']][$day] = "";
    }

    if (array_key_exists($day, $timetable_classes)) {
        $event_entry = $timetable_classes[$time_value['time']];
        foreach($event_entry as $event_day => $events) {
            $result_array[$time_value['time']][$day][] = $events;
        }
    }
}

print_r($result_array);

我很感激帮助。

这就是我想要的结果

$arr = array(
    "06:00:00" => array(
                                                    "Sunday" => array(
                                                            array(
                                                                'event_title' => "item_1",
                                                                'event_desc' => "item_1",
                                                                'event_link' => "item_1",
                                                            ),
                                                        ),  
                                                        "Monday" => array(
                                                                array(
                                                                        'event_title' => "item_1",
                                                                        'event_desc' => "item_1",
                                                                    'event_link' => "item_1",
                                                                        ),                                                                  
                                                                        array(
                                                                            'event_title' => "item_1",
                                                                            'event_desc' => "item_1",
                                                                            'event_link' => "item_1",
                                                                            ),
                                                                        ), 
                                                        "Tuesday" => "", 
                                                        "Wednesday" => "", 
                                                        "Thursday" => "", 
                                                        "Friday" => "", 
                                                        "Saturday" => ""
                                                        ),
                                     "07:00:00" => array("Sunday" => "", "Monday" => "", "Tuesday" => "", "Wednesday" => "", "Thursday" => "", "Friday" => "", "Saturday" => ""),
                                     "08:00:00" => array("Sunday" => "", "Monday" => "", "Tuesday" => "", "Wednesday" => "", "Thursday" => "", "Friday" => "", "Saturday" => ""),
                                     "09:30:00" => array("Sunday" => "", "Monday" => "", "Tuesday" => "", "Wednesday" => "", "Thursday" => "", "Friday" => "", "Saturday" => ""),
                                     "17:00:00" => array("Sunday" => "", "Monday" => "", "Tuesday" => "", "Wednesday" => "", "Thursday" => "", "Friday" => "", "Saturday" => ""),
                                     );
4

1 回答 1

1

首先创建一个函数,它将在您的列表中搜索与工作日和时间段匹配的条目(这个带有闭包的实现需要 php 5.3,但对于旧版本,实现应该是微不足道的)

<?php
function getClassesAt($day, $timeslot, $classes){
    $matches = array_filter($classes, function($class) use ($day, $timeslot){
        return ($class->class_day == $day && $class->class_time == $timeslot);
    });
    if(count($matches) == 0) return "";
    $ret = array();

    foreach($matches as $match){
        $ret[] = array("title"=>$class->class_name,
                        "description"=>$class->class_description,
                        "link"=>"???");
    }
    return $ret;
}

然后构建一个容器数组,其中包含每个会话密钥的工作日密钥数组

$days = array("Monday"=>"","Tuesday"=>"","Wednesday"=>"","Thursday"=>"","Friday"=>"","Saturday"=>"","Sunday"=>"");
$results = array_combine($sessions, array_pad(array(), count($sessions), $days));

上述代码最后一行的替代方法是

$results = array();
foreach ($sessions as $session){
     $results[$session] = $days;
}

然后遍历该数组的叶子,调用并分配 getClassesAt 函数。

foreach($results as $timeslot =>&$weekdays){
    foreach ($weekdays as $weekday=>$res){
        $weekdays[$weekday] = getClassAt($weekday, $timeslot, $classes);
    }
}

警告这只是部分测试,所以我只有大约 60% 的把握它会做你想要的,但它所基于的逻辑可能会为你指明正确的方向。

更新

根据新提供的代码,这里是一个可行的解决方案。

<?php
$classes = array(
                    array(
                        "class_id" => 1,
                        "class_name" => "Fit Box",
                        "class_description" => "description here",
                        "class_time" => "06:00:00",
                        "class_day" => "Tuesday",
                        "class_status" => "active",
                        "class_colour" => "blue",
                    ),
                    array(
                        "class_id" => 2,
                        "class_name" => "Hot Box",
                        "class_description" => "description here",
                        "class_time" => "08:00:00",
                        "class_day" => "Wednesday",
                        "class_status" => "active",
                        "class_colour" => "Red",
                    ),
                    array(
                        "class_id" => 3,
                        "class_name" => "Punch Face",
                        "class_description" => "description here",
                        "class_time" => "09:00:00",
                        "class_day" => "Thursday",
                        "class_status" => "active",
                        "class_colour" => "blue",
                    ),
                    array(
                        "class_id" => 4,
                        "class_name" => "MOS",
                        "class_description" => "description here",
                        "class_time" => "19:30:00",
                        "class_day" => "Monday",
                        "class_status" => "active",
                        "class_colour" => "blue",
                    ),
                    array(
                        "class_id" => 5,
                        "class_name" => "Yoga",
                        "class_description" => "description here",
                        "class_time" => "08:00:00",
                        "class_day" => "Wednesday",
                        "class_status" => "active",
                        "class_colour" => "blue",
                    ),
                );
$timetable_times = array(
                    array(
                        "time_id" => 1,
                        "time" => "06:00:00",
                    ),
                    array(
                        "time_id" => 2,
                        "time" => "07:00:00",
                    ),
                    array(
                        "time_id" => 3,
                        "time" => "08:00:00",
                    ),
                    array(
                        "time_id" => 4,
                        "time" => "09:00:00",
                    ),
                    array(
                        "time_id" => 5,
                        "time" => "09:30:00",
                    ),
                    array(
                        "time_id" => 6,
                        "time" => "17:30:00",
                    ),
                    array(
                        "time_id" => 7,
                        "time" => "18:00:00",
                    ),
                    array(
                        "time_id" => 8,
                        "time" => "18:30:00",
                    ),
                    array(
                        "time_id" => 9,
                        "time" => "19:30:00",
                    ),
                );  


function getClassesAt($day, $timeslot, $classes){
    $matches = array_filter($classes, function($class) use ($day, $timeslot){
        return ($class["class_day"] == $day && $class["class_time"] == $timeslot);
    });
    if(count($matches) == 0) return "";
    $ret = array();

    foreach($matches as $match){
        $ret[] = array("title"=>$match["class_name"],
                        "description"=>$match["class_description"],
                        "link"=>"???");
    }
    return $ret;
}


$sessions = array_map(function($a){return $a["time"]; }, $timetable_times);    
$days = array("Monday"=>"","Tuesday"=>"","Wednesday"=>"","Thursday"=>"","Friday"=>"","Saturday"=>"","Sunday"=>"");
$results = array_combine($sessions, array_pad(array(), count($sessions), $days));

foreach($results as $timeslot =>&$weekdays){
    foreach ($weekdays as $weekday=>$res){
        $weekdays[$weekday] = getClassesAt($weekday, $timeslot, $classes);
    }
}

print_r($results);

输出是

Array
(
    [06:00:00] => Array
        (
            [Monday] => 
            [Tuesday] => Array
                (
                    [0] => Array
                        (
                            [title] => Fit Box
                            [description] => description here
                            [link] => ???
                        )

                )

            [Wednesday] => 
            [Thursday] => 
            [Friday] => 
            [Saturday] => 
            [Sunday] => 
        )

    [07:00:00] => Array
        (
            [Monday] => 
            [Tuesday] => 
            [Wednesday] => 
            [Thursday] => 
            [Friday] => 
            [Saturday] => 
            [Sunday] => 
        )

    [08:00:00] => Array
        (
            [Monday] => 
            [Tuesday] => 
            [Wednesday] => Array
                (
                    [0] => Array
                        (
                            [title] => Hot Box
                            [description] => description here
                            [link] => ???
                        )

                    [1] => Array
                        (
                            [title] => Yoga
                            [description] => description here
                            [link] => ???
                        )

                )

            [Thursday] => 
            [Friday] => 
            [Saturday] => 
            [Sunday] => 
        )

    [09:00:00] => Array
        (
            [Monday] => 
            [Tuesday] => 
            [Wednesday] => 
            [Thursday] => Array
                (
                    [0] => Array
                        (
                            [title] => Punch Face
                            [description] => description here
                            [link] => ???
                        )

                )

            [Friday] => 
            [Saturday] => 
            [Sunday] => 
        )

    [09:30:00] => Array
        (
            [Monday] => 
            [Tuesday] => 
            [Wednesday] => 
            [Thursday] => 
            [Friday] => 
            [Saturday] => 
            [Sunday] => 
        )

    [17:30:00] => Array
        (
            [Monday] => 
            [Tuesday] => 
            [Wednesday] => 
            [Thursday] => 
            [Friday] => 
            [Saturday] => 
            [Sunday] => 
        )

    [18:00:00] => Array
        (
            [Monday] => 
            [Tuesday] => 
            [Wednesday] => 
            [Thursday] => 
            [Friday] => 
            [Saturday] => 
            [Sunday] => 
        )

    [18:30:00] => Array
        (
            [Monday] => 
            [Tuesday] => 
            [Wednesday] => 
            [Thursday] => 
            [Friday] => 
            [Saturday] => 
            [Sunday] => 
        )

    [19:30:00] => Array
        (
            [Monday] => Array
                (
                    [0] => Array
                        (
                            [title] => MOS
                            [description] => description here
                            [link] => ???
                        )

                )

            [Tuesday] => 
            [Wednesday] => 
            [Thursday] => 
            [Friday] => 
            [Saturday] => 
            [Sunday] => 
        )

)
于 2013-07-12T03:12:44.560 回答