0

要从 API 检索数据,我正在使用一个 php 库,该库返回一个包含无序事件信息的对象。

每个事件返回一周中每一天(星期一、星期二等)的布尔值。

认为这个想法是:

  1. 迭代对象并测试工作日的每个事件(周日、周一等)如果为真
    1. 为事件创建并填充一系列所需的详细信息
    2. 为工作日创建并填充一组事件数组
    3. 每个工作日按事件时间排序
    4. 显示天事件

现在正在使用:

if ($class->DaySunday == 1) 
{   //initialize or increment counter
!isset($num_classes) ? $num_classes = 0 : $num_classes++;
//assemble Class details into array containing the day name
$single_class = build_class('sunday',$class,$num_classes);
//Create and populate @Day Schedule with Single Class arrays 
foreach ($single_class as $class => $details)
    $sunday_schedule[$num_classes][$class] = $details;
//Sort the Day Schedule by Hour, earliest to latest
usort($sunday_schedule, "sortFunction");
} 
if ($class->DayMonday == 1) {}//etc...

其中 build_class() 为每个类返回一个数组:

function build_class($day_name,$class,$num_classes)
{
//Create and populate @Single Class details from ClassSchedule Object 
$single_class['Class Name'] = $class->ClassDescription->Name;
return $single_class;
}

似乎为每一天创建事件数组的数组应该能够在为每个类创建数组的同一个函数中发生,但我似乎无法让它工作,因为它要么从每天创建一个巨大的事件数组OR 覆盖事件,以便每天只剩下一个事件。

为每个 IF_is_particular_weekday 函数重复上述五行代码是丑陋的,必须有更有效的方法。我需要动态构建每个 Day 数组的名称吗?

我什至不确定从对象创建数组是否是最有效的方法。

换句话说:

动态生成变量名 no。将数组添加到另一个数组。所以@the week 包含@the days 包含@the 类:

Array (
    [monday] => Array (
        [0] => Array (
            [Class Name] => Work Out
            [Instructor] => General Meany
        )
    [tuesday] => Array (
        [0] => Array (
            [Class Name] => Dance
            [Instructor] => Ballerina
        )
    )
)

这是我的第一篇 stackoverflow 帖子,我欢迎(也害怕)你的见解。-谢谢。

这是调用和函数:

if ($class->DaySunday == 1) 
{//initialize or increment counter for specific day
!isset($sun_class_count) ? $sun_class_count = 0 : $sun_class_count++;
$day_name = 'Sunday';
//send Day, Object, Day Count and class and day arrays to build week function
$week_schedule = build_week($day_name,$class,$sun_class_count,$single_class,$week_schedule);
} 
if ($class->DayMonday == 1)
{!isset($mon_class_count) ? $mon_class_count = 0 : $mon_class_count++;
$day_name = 'Monday';}//etc

function build_week($day_name,$class,$class_count,$single_class,$week_schedule)
{
//Create and populate @Single Class details from ClassSchedule Object 
    $single_class['Class Name'] = $class->ClassDescription->Name;
    $single_class['Instructor'] = $class->Staff->Name; 
    $single_class['Class Type'] = $class->ClassDescription->SessionType->Name; 
$single_class['Start Time'] = $class->StartTime;  
$single_class['End Time'] = $class->EndTime;  
$single_class['Day Name'] = $day_name; 
$week_schedule[$day_name][$class_count] =  $single_class;

return $week_schedule;          }
4

0 回答 0