reposition array elements based on a condition in php
So I have this array:
$dates[0] = array('start'=>'2010-07-22', 'end'=>'2010-07-23');
$dates[1] = array('start'=>'2010-07-22', 'end'=>'0000-00-00');
$dates[2] = array('start'=>'2010-07-29', 'end'=>'0000-00-00');
$dates[3] = array('start'=>'2010-07-31', 'end'=>'2010-07-31');
$dates[4] = array('start'=>'2010-07-08', 'end'=>'2010-07-31');
$dates[5] = array('start'=>'2010-08-01', 'end'=>'2010-09-30');
$dates[6] = array('start'=>'2010-07-18', 'end'=>'2010-08-15');
$dates[7] = array('start'=>'2010-07-01', 'end'=>'2010-08-31');
and i have the following condition:
if an element 'start' is less then today, took the element's attribute 'end' and position the element relative to the others elements 'start'
So if an event started yesterday
and ends tomorrow
it should appear after the events beginning today
so the resulting array is something like this:
$dates[3] = array('start'=>'2010-07-22', 'end'=>'2010-07-23');
$dates[4] = array('start'=>'2010-07-22', 'end'=>'0000-00-00');
$dates[5] = array('start'=>'2010-07-29', 'end'=>'0000-00-00');
$dates[6] = array('start'=>'2010-07-31', 'end'=>'2010-07-31');
$dates[1] = array('start'=>'2010-07-08', 'end'=>'2010-07-31');
$dates[7] = array('start'=>'2010-08-01', 'end'=>'2010-09-30');
$dates[2] = array('start'=>'2010-07-18', 'end'=>'2010-08-15');
$dates[0] = array('start'=>'2010-07-01', 'end'=>'2010-08-31');
How could i go from the sample input to the sample output?
[EDIT]
- I'm trying to change the output of EventList a component for Joomla
- I'm not a native english speaker
So, I'll try again:
if the event start date is minor than today then order the event relative to this event enddate
and the others events startday
Ex:
If the event 'A' started on 2010-07-01 and finish on 2010-07-25 and event 'B' starts on 2010-07-24 and the current date is 2010-07-20 then event 'A' comes after event 'B'
If the event 'A' starts on 2010-07-20 and finish on 2010-07-25 and event 'B' starts on 2010-07-24 and the curren开发者_开发技巧t date is 2010-07-20 then event 'B' comes after event 'A'
I 'hope' it's a little more clearer now.
Hmmm, so if I understand correctly: sort by stardate if >=today, enddate if startdate <= today.
function _myDateSorter($a,$b){
$date = date('Y-m-d');
$adate = ($a['start'] >= $date) ? $a['start'] : $a['end'];
$bdate = ($b['start'] >= $date) ? $b['start'] : $b['end'];
if($adate =='0000-00-00') $adate = '9999-99-99';//sort after it appears
if($bdate =='0000-00-00') $bdate = '9999-99-99';//sort after it appears
if($adate == $bdate) return 0;
return $adate > $bdate ? 1 : -1;
}
uasort($dates,'_myDateSorter');
精彩评论