Possible to sort complex PHP for loop

2019-09-16 02:46发布

Is it possible to sort a complex for loop? Would I store the values in an array and then sort by that?

This is the for loop I'm working with, I would like to sort by the date & time of the events I'm pulling through

foreach ($recurring_events as $recurring_event):
    if ($recurring_event->period == 'week') { 
        $StartDate = strtotime($event->RequestDateStart);
        $EndDate = strtotime($event->RequestDateEnd);
        $TotalDays = round(($EndDate-$StartDate)/(60*60*24*7));
        for($i=0; $i<($TotalDays-1); $i++) {
            $StartDate += (60*60*24*7);
            if (date('WMY', $StartDate) == date('WMY')) {
                echo '<div class="col-12">';

                echo '<strong>Event Name:</strong> ' . $event->EventName . '<br /><strong>Date:</strong> ' . date('l, F j o',$StartDate) . '<br /><strong>Start Time:</strong> ' . date('g:i a',strtotime($event->RequestTimeStart));

                echo '</div>';
            }
        }
    }
endforeach; 

4条回答
成全新的幸福
2楼-- · 2019-09-16 03:04

It's not possible to sort the data while looping round it to output it. Instead, sort the data, then loop round the sorted data to output.

A useful function is uasort which takes an array and uses a function you define to sort it:

function sortEvents($a, $b) {
   if (strtotime($a->RequestDateStart) == strtotime($b->RequestDateStart)) {
      return 0;
   }
   return ($a->RequestDateStart < $b->RequestDateStart) ? -1 : 1;
}
uasort($recurring_events,'sortEvents');
查看更多
祖国的老花朵
3楼-- · 2019-09-16 03:12
foreach ($recurring_events as $recurring_event):
    if ($recurring_event->period == 'week') { 
        $StartDate = strtotime($event->RequestDateStart);
        $EndDate = strtotime($event->RequestDateEnd);
        $TotalDays = round(($EndDate-$StartDate)/(60*60*24*7));
        for($i=0; $i<($TotalDays-1); $i++) {
            $EndDate -= (60*60*24*7);
            if (date('WMY', $StartDate) == date('WMY')) {
                echo '<div class="col-12">';

                echo '<strong>Event Name:</strong> ' . $event->EventName . '<br /><strong>Date:</strong> ' . date('l, F j o',$StartDate) . '<br /><strong>Start Time:</strong> ' . date('g:i a',strtotime($event->RequestTimeStart));

                echo '</div>';
            }
        }
    }
endforeach;

try this start from the end date and minus it.if you want it ti be in decsnding sorry if i dint understood you question

查看更多
倾城 Initia
4楼-- · 2019-09-16 03:23

You need to put your results into an array, then sort it, then output it.

$output = array();
foreach ($recurring_events as $recurring_event):
    if ($recurring_event->period == 'week') { 
        $StartDate = strtotime($event->RequestDateStart);
        $EndDate = strtotime($event->RequestDateEnd);
        $TotalDays = round(($EndDate-$StartDate)/(60*60*24*7));
        for($i=0; $i<($TotalDays-1); $i++) {
            $StartDate += (60*60*24*7);
            if (date('WMY', $StartDate) == date('WMY')) { //$StartDate >= strtotime('now') &&
                $output[$StartDate] .= '<div class="col-12">';

                $output[$StartDate] .='<strong>Event Name:</strong> ' . $event->EventName . '<br /><strong>Date:</strong> ' . date('l, F j o',$StartDate) . '<br /><strong>Start Time:</strong> ' . date('g:i a',strtotime($event->RequestTimeStart));

                $output[$StartDate] .='</div>';
            }
        }
    }
endforeach; 

And then use sort

sort($output);

Assuming that your StartDate is the date you need (then it should be an unix timestampe?)

http://php.net/manual/en/function.sort.php

查看更多
太酷不给撩
5楼-- · 2019-09-16 03:24

put it in an array, then refer to this post for how to sort by date

How to sort a date array in PHP

查看更多
登录 后发表回答