Reputation: 61
I have an array of events.
$events = array(
array(
'title'=>'event title',
'startDate'=> '2014-03-12',
'endDate'=> '2014-03-12',
'description'=> 'event description'
),
array(
'title'=>'event title',
'startDate'=> '2013-03-12',
'endDate'=> '2013-05-23',
'description'=> 'event description'
)
);
and I need to split them into arrays by year, and then by month.
So like :
array(
'year'=>'2014',
'months'=>
array(
'Jan',
'events'=>
array(
'title'=>'event title',
'startDate'=> '2014-01-12',
'endDate'=> '2014-01-12',
'description'=> 'event description'
),
array(
'title'=>'event title',
'startDate'=> '2014-03-12',
'endDate'=> '2014-03-12',
'description'=> 'event description'
)
)
),
I'm not a pro at programming and PHP so any help would be great.
Upvotes: 1
Views: 1399
Reputation: 1767
The following code should produce something which you could use:
$ordered_events = array();
foreach ($events as $event) {
list($startYear, $startMonth, $startDay) = split("-", $event['startDate']);
$ordered_events[$startYear][$startMonth][$startDay][] = $event;
if ($event['startDate'] != $event['endDate']) {
list($endYear, $endMonth, $endDay) = split("-", $event['endDate']);
$ordered_events[$endYear][$endMonth][$endDay][] = $event;
}
}
Example output:
Array(
[2014] => Array(
[03] => Array(
[12] => Array(
[0] => Array(
[title] => event title
[startDate] => 2014-03-12
[endDate] => 2014-03-12
[description] => event description
)
)
)
)
[2013] => Array(
[03] => Array(
[12] => Array(
[0] => Array(
[title] => event title
[startDate] => 2013-03-12
[endDate] => 2013-05-23
[description] => event description
)
)
)
[05] => Array(
[23] => Array(
[0] => Array(
[title] => event title
[startDate] => 2013-03-12
[endDate] => 2013-05-23
[description] => event description
)
)
)
)
)
// Updated to add endDate.
Upvotes: 1
Reputation: 61
Thanks Johan that worked, one issue i forgot to mention is that an event has a start and an end date. Which means it can span across more than one month. How would i go about sorting them into months, if the end date finishes in the next month along, it would need to show up in both months.
Appreciate all the help, you guys are life savers!
Upvotes: 1
Reputation: 9635
try this
<?php
echo "<pre>";
$events = array(
array(
'title'=>'event title',
'startDate'=> '2014-01-12',
'endDate'=> '2014-01-12',
'description'=> 'event description'
),
array(
'title'=>'event title',
'startDate'=> '2014-01-15',
'endDate'=> '2014-01-16',
'description'=> 'event description'
),
array(
'title'=>'event title',
'startDate'=> '2014-03-9',
'endDate'=> '2014-03-12',
'description'=> 'event description'
),
array(
'title'=>'event title',
'startDate'=> '2013-03-12',
'endDate'=> '2013-03-23',
'description'=> 'event description'
),
array(
'title'=>'event title',
'startDate'=> '2013-03-31',
'endDate'=> '2013-03-23',
'description'=> 'event description'
)
);
echo "<b>Input : </b>";
print_r($events);
$arr_output = array();
foreach($events as $key=>$arr)
{
$date = $arr['startDate'];
$arr_dates = explode("-", $date);
$year = $arr_dates[0];
$month = $arr_dates[1];
$arr_output[$year][$month][] = $arr;
}
echo "<b>Output : </b>";
print_r($arr_output);
OUTPUT ARRAY:
Array
(
[2014] => Array
(
[01] => Array
(
[0] => Array
(
[title] => event title
[startDate] => 2014-01-12
[endDate] => 2014-01-12
[description] => event description
)
[1] => Array
(
[title] => event title
[startDate] => 2014-01-15
[endDate] => 2014-01-16
[description] => event description
)
)
[03] => Array
(
[0] => Array
(
[title] => event title
[startDate] => 2014-03-9
[endDate] => 2014-03-12
[description] => event description
)
)
)
[2013] => Array
(
[03] => Array
(
[0] => Array
(
[title] => event title
[startDate] => 2013-03-12
[endDate] => 2013-03-23
[description] => event description
)
[1] => Array
(
[title] => event title
[startDate] => 2013-03-31
[endDate] => 2013-03-23
[description] => event description
)
)
)
)
Upvotes: 1