I'm trying to create a rather complex array for my calendar application. It's supposed to contain dates, with the name of the day, the 'type' and the events, if any. I got as far as creating this:

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

Now I would like to add another key called 'events'. I want to add the events for each day to the array.. So I could get something like:

        [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]
                      }
              }

Not sure if this makes sense.

I created the first example using this code:

        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;

I then wanted to grab the event info by doing:

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

in the same for loop.

My get_event_info method looks like this:

    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;

}

It outputs arrays like this:

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

Now, I return the $event_info from get_events_info to the create_dates_list method, but I'm not sure how I would go about adding my event arrays to the $dates array.

The get_event_info gets the events for each date (1 - end of month-). I then want to add those to my $dates array in the format listed above in my second example.

I'm confused cause these are rather complex arrays. Thanks in advance.

Accepted Answer

I think all you need to do is (for example):

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

Then you can access your elements:

 $array['dates']['1']['event'][$id]['title']    
 $array['dates']['1']['event'][$id]['description']    
Written by afuzzyllama
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki