有没有办法将array_multisort与自定义顺序一起使用?我需要以日期顺序显示结果,第一个日期是最接近今天的日期,正如您可以从下面的代码中看到,matchDate字段以字符串形式出现,我稍后将其转换为日期值。
foreach($matchLists as $matchList)
{
$fixtures[] = $matchList['matchDate'];
}
array_multisort($fixtures, SORT_DESC, $matchLists);
$newlist = array();
foreach($matchLists as $key => $matchitem)
{
if(array_key_exists('matchDate', $matchitem))
{
$newlist[$matchitem['matchDate']][$key] = ($matchitem);
}
}
foreach($newlist as $key => $value)
{
$fixtureDate = date('D j M Y ga', strtotime($key));
}
答案 0 :(得分:0)
是的,请看一下我的previous answer on SO:
<?php
$events = array(
'event1' => array(
'event_name' => 'Title for Event 1',
'event_date' => '2017-11-1'
),
'event3' => array(
'event_name' => 'Title for Event 1',
'event_date' => '2017-10-13'
),
'event4' => array(
'event_name' => 'Title for Event 1',
'event_date' => '2017-11-10'
),
'event2' => array(
'event_name' => 'Title for Event 1',
'event_date' => '2017-10-22'
),
);
function date_compare($a, $b)
{
// note that the variables are calling for the date part of the array
// if you are using assoc array from mysql just change the value
// to your row name
$t1 = strtotime($a['event_date']);
$t2 = strtotime($b['event_date']);
return $t1 - $t2;
}
usort($events, 'date_compare');
print_r($events);
这里有一个包含日期的数组,您可以创建date_compare
函数,然后usort
根据日期对数组进行排序。
希望这有帮助