我想要实现的是显示所有会话时间并且每次都有所有工作日,然后每周工作日会显示一个或多个事件。
我可以获得时间和工作日,但不能获得那些日子的事件。我提供了我的数组和代码,看看我是否能得到一些帮助。
我的会话时间
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" => ""),
);
答案 0 :(得分: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] =>
)
)