2013-02-13 76 views
-3

我正在为音乐推广公司制作网站。对于每一个艺术家,我有一个包含事件信息的一个单独的阵列,像这样:如何按时间顺序排列PHP数组与关联?

<?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", 
    ); 


?> 

我想所有这些不同的阵列相结合,利用包括,在一个活动页面,并让它们按时间顺序排列。有没有办法做到这一点与“日期”关联数组?

所以它最终会是这样的: 2013年3月18日 唐·罗斯 半月,普特尼

二零一三年三月二十日 布鲁克·米勒 流浪汉,布里

2013年3月29日 吉米·瓦赫勒斯廷 斯旺西的车库

+1

重复所有“数组排序”问题。 – 2013-02-13 17:30:52

+1

你有没有做过任何一种努力? – inhan 2013-02-13 17:32:03

回答

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; 
}); 
+0

非常感谢。我是新手,我不知道从哪里开始寻找这些特定的功能。 – user2059458 2013-02-13 18:35:30