我正在为一家音乐推广公司建立一个网站。对于每个艺术家,我都有一个包含事件信息的单独数组,如下所示:
<?php
$donevents = array();
$donevents[101] = array(
"date" => "March 18th 2013",
"artist" => "Don Ross",
"venue" => "The Half Moon, Putney",
"link" => "http://www.halfmoon.co.uk",
"label" => "donross",
);
$donevents[102] = array(
"date" => "March 19th 2013",
"artist" => "Don Ross",
"venue" => "The Moon Club, Cardiff",
"link" => "http://www.themoonclub.net/",
"label" => "donross",
);
$donevents[103] = array(
"date" => "March 21st 2013",
"artist" => "Don Ross and Brooke Miller",
"venue" => "Round Midnight, London",
"link" => "http://www.roundmidnightbar.com",
"label" => "donross",
);
$donevents[104] = array(
"date" => "March 21st 2013",
"artist" => "Don Ross",
"venue" => "Masterclass at the Swindon Academy of Music",
"link" => "http://www.academyofmusic.ac.uk/locations/location.php?id=9",
"label" => "donross",
);
$donevents[105] = array(
"date" => "March 23rd 2013",
"artist" => "Don Ross",
"venue" => "Berits & Brown, Airdrie",
"link" => "http://www.intimate-gigs.com",
"label" => "donross",
);
$donevents[106] = array(
"date" => "March 25th 2013",
"artist" => "Don Ross",
"venue" => "Masterclass at The Academy of Music, Gateshead",
"link" => "http://www.guitarbar.co.uk",
"label" => "donross",
);
?>
我想将所有这些单独的数组组合在一起,使用包含在一个事件页面中并按时间顺序排序。有没有办法用“date”关联数组做到这一点?
所以最终会像这样: 2013年3月18日 唐罗斯 半月,普特尼
2013年3月20日 布鲁克米勒 霍布斯,布里真德
2013年3月29日 Jimmy Wahlsteen 车库,斯旺西
答案 0 :(得分:1)
是的,您需要使用uasort函数,如下所示:
$sortedEvents = $donevents;
uasort($sortedEvents, function($eventA, $eventB){
$eventDateA = DateTime::createFromFormat('F dS yyyy', $eventA['date']);
$eventDateB = DateTime::createFromFormat('F dS yyyy', $eventB['date']);
return $eventA->getTimestamp() < $eventB->getTimestamp() ? -1 : 1;
});