我有数组$leaderboard。包含以下数据
Array
(
[0] => Array
(
[name] => ABC
[time_taken] => 01:46:56
)
[1] => Array
(
[name] => DEF
[time_taken] => 00:21:54
)
[2] => Array
(
[name] => MNO
[time_taken] => 00:34:14
)
[3] => Array
(
[name] => PQR
[time_taken] => 00:09:17
)
[4] => Array
(
[name] => XYZ
[time_taken] => 00:34:14
)
[5] => Array
(
[name] => STR
[time_taken] => 00:34:14
)
[6] => Array
(
[name] => LOK
[time_taken] => 17:53:58
)
}
我试着根据时间对数组进行排序。如果使用ksort时时间值不重复,则工作正常。但对于重复的时间,它只显示在的记录上
<h4>function aasort (&$array, $key) {
<br>
$sorter=array();<br>
$ret=array();<br>
reset($array);<br>
foreach ($array as $ii => $va) {<br>
$sorter[$ii]=$va[$key];<br>
<br> }
asort($sorter);<br>
foreach ($sorter as $ii => $va) <br>{
$ret[$ii]=$array[$ii];
}
$array=$ret;<br>
}
aasort($your_array,"order");</h4>
试试这个代码,
<?php
$sortArray = array(array('name' => 'ABC',
'time_taken' => '01:46:56'),
array('name' => 'DEF',
'time_taken' => '00:21:54'),
array('name' => 'MNO',
'time_taken' => '00:34:14'),
array('name' => 'PQR',
'time_taken' => '00:09:17'),
array('name' => 'XYZ',
'time_taken' => '00:34:14'),
array('name' => 'LOK',
'time_taken' => '17:53:58'));
print_r($sortArray);
foreach ($sortArray as $key => $row) {
$sorting[$key] = $row['time_taken'];
}
array_multisort($sorting, SORT_ASC, $sortArray);
print_r($sortArray);
您可以定义自己的排序函数并使用uksort。更多信息:uksort
请尝试一下。。
function cmp( $a, $b ) {
if( strtotime($a['time_taken']) == strtotime($b['time_taken']) ){ return 0 ; }
return (strtotime($a['time_taken']) < strtotime($b['time_taken'])) ? -1 : 1;
}
usort($leaderboard,'cmp');
参考https://stackoverflow.com/a/9001655/829533