简体   繁体   English

PHP数组合并

[英]PHP Array combining

What i am trying to achieve is to display all the session times and have each time with all the week days, then each week day will show one or more events if they have them. 我要达到的目的是显示所有会话时间,并在每个工作日都显示所有时间,然后每个工作日将显示一个或多个事件(如果有)。

I can get the times and week days but not the events for those days. 我可以获取时间和工作日,但不能获取那些日的事件。 i have provided my arrays and code to see if i can get some help. 我提供了我的数组和代码,以查看是否可以获得帮助。

My Session Times 我的会议时间

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
)

My Events 我的活动

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
)

)

And this is my code that tries to do the job. 这是我的代码,试图完成这项工作。

    $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);

I appreciate the help. 感谢您的帮助。

this is the outcome i am aiming for 这是我想要的结果

$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" => ""),
                                     );

First off create a function that will search your list for entries matching the weekday and timeslot (this implementation with closures requires php 5.3 but for older versions the implementation should be trivial) 首先创建一个函数,该函数将在您的列表中搜索与工作日和时间段匹配的条目(带有闭包的此实现需要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;
}

Then build a container array that has an array with weekday keys for each session key 然后构建一个容器数组,该数组具有一个包含每个会话键的工作日键的数组

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

an alternative to the last line of the above code would be 上面代码最后一行的替代方法是

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

Then walk the leafs of that array calling and assigning the the getClassesAt function. 然后遍历该数组的叶子,调用并分配getClassesAt函数。

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

Warning this was only partially tested so I'm only about 60% sure that it will do what you want but the logic it's based on might point you in the right direction. 警告这只是部分测试,因此我只有大约60%的人确定它会做您想要的事情,但基于它的逻辑可能会为您指明正确的方向。

UPDATE UPDATE

Based on the newly supplied code here is a working solution. 根据新提供的代码,这里是一个有效的解决方案。

<?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);

Output is 输出是

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] => 
        )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM