2013-12-16 46 views
-2

我有數組$排行榜。其中包含以下數據如何按數組值排序多維數組。數組可能包含重複的值

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 
     ) 
} 

我試着按時間對數組進行排序。它的工作正常,如果時間值不重複使用kso​​rt。但是對於重複的時間,它只顯示在記錄中

+0

直視[usort(HTTP ://www.php.net/manual/en/function.usort.php)或類似 – gwillie

回答

0

試試這個代碼,

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

謝謝@Ajith S.現在正在工作。 –

0

您可以定義自己的排序功能並使用uksort。 更多有關:uksort

1
<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> 
0

試試看請..

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

+0

更簡短的方法是返回'strtotime($ a ['time_taken']) - strtotime($ b ['time_taken'])' –

+0

@zzlalani謝謝 –

+0

@Nitin_aeon是否適合您? – zzlalani