Reputation: 3773
I have an array as below. I simply want to order the echoing of it by date, so we have the most recent date at the top. I'm not sure though if a. php can order arrays, and b. whether it can read the date in the format it is in. I can change the format it is capture din if that would make it easier?
Array (
[0] => Array (
[month_year] => September2012
[vimeo_link] => http://vimeo.com/472
)
[1] => Array (
[month_year] => July2012
[vimeo_link] => https://vimeo.com/460
)
[2] => Array (
[month_year] => August2012
[vimeo_link] => https://vimeo.com/490
)
)
foreach($rows as $row){?>
<li><a target="_blank" href="<?php echo $row['vimeo_link'];?>"><?php echo $row['month_year'];?></a></li>
<?php
}?>
Upvotes: 0
Views: 58
Reputation: 95101
You can try
$rows = Array(
"0" => Array("month_year" => "September2012","vimeo_link" => "http://vimeo.com/472"),
"1" => Array("month_year" => "July2012","vimeo_link" => "https://vimeo.com/460"),
"2" => Array("month_year" => "August2012","vimeo_link" => "https://vimeo.com/490"));
usort($rows, function ($a, $b) {
$a = DateTime::createFromFormat("FY", $a['month_year']);
$b = DateTime::createFromFormat("FY", $b['month_year']);
return ($a == $b) ? 0 : (($a < $b) ? - 1 : 1);
});
foreach ( $rows as $row ) {
printf("<li><a target=\"_blank\" href=\"%s\">%s</a></li>", $row['vimeo_link'], $row['month_year']);
}
Output
Upvotes: 1
Reputation: 29160
You'll need to convert those month/date string into valid dates or timestamps, then you will want to look at usort()
http://php.net/manual/en/function.usort.php
Upvotes: 0