在CodeIgniter中使用带有Calendar类的数组

时间:2011-04-23 15:15:57

标签: php arrays codeigniter calendar

我正在尝试为我的日历应用程序创建一个相当复杂的数组。它应该包含日期,日期的名称,“类型”和事件(如果有的话)。我创造了这个:

[dates] {
        [22] {
             [day] => [Friday]
             [type] => [weekday]                              
        }
        [23] {
             [day] => [Saturday]
             [type] => [Weekend]
        }
        [24] {
             [day] => [Sunday]
             [type] => [Weekend]
        }
}

现在我想添加另一个名为'events'的键。我想将每天的事件添加到数组中。所以我可以得到类似的东西:

        [24] {
             [day] => [Sunday]
             [type] => [Weekend]
             [events] {
                      [1] {
                          [title] => [test event]
                          [description] => [my event]
                          [date] => [24-04-2011]
                      }
                      [2] {
                          [title] => [test event 2]
                          [description] => [my second event]
                          [date] => [24-04-2011]
                      }
              }

不确定这是否有意义。

我使用此代码创建了第一个示例:

        for($i = 1; $i <= $data['days_in_curr_month']; $i++)
    {
        $day_info = $this->get_day_info($i, $data['current_month'], $data['current_year']);

        $dates[$i]['name'] = $day_info['day_name'];
        $dates[$i]['type'] = $day_info['day_type'];
    }

    return $dates;

然后我想通过执行以下操作获取事件信息:

$event_info = $this->get_event_info($i, $data['current_month'], $data['current_year']);

在同一个for循环中。

我的get_event_info方法如下所示:

    public function get_event_info($day, $month, $year)
{
    $date = $year . "-" . $month . "-" . $day;
    $this->db->where('date', $date);
    $this->db->where('user_id', '1');   
    $query = $this->db->get('tblEvents');

    $event_info = array();

    foreach($query->result() as $row)
    {
        $event_info['events'][$row->id]['title'] = $row->title;
        $event_info['events'][$row->id]['description'] = $row->description;
    }

    return $event_info;

}

它输出如下数组:

Array ( [events] => Array ( [1] => Array ( [title] => Project 2 [description] => Test: Project 2 ) [2] => Array ( [title] => Illustrator [description] => Test: Illustrator 2 ) ) )

现在,我将$ event_info从get_events_info返回到create_dates_list方法,但我不确定如何将我的事件数组添加到$ dates数组中。

get_event_info获取每个日期的事件(1 - 月末)。然后我想在我的第二个例子中以上面列出的格式将它们添加到我的$ dates数组中。

我很困惑因为这些是相当复杂的数组。提前谢谢。

1 个答案:

答案 0 :(得分:2)

我认为您需要做的就是(例如):

 $append_events = get_event_info($day, $month, $year);
 $array['dates']['1']['event'] = $append_events['events'];

然后你可以访问你的元素:

 $array['dates']['1']['event'][$id]['title']    
 $array['dates']['1']['event'][$id]['description']