如何按数组值排序php顺序的多维数组。数组可能包含重复的值

时间:2013-12-16 06:42:28

标签: php sorting multidimensional-array

我有阵列$排行榜。其中包含以下数据

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时间值不重复,它的工作正常。但是对于重复的时间,它只显示来自

的记录

4 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

您可以定义自己的排序功能并使用uksort。 更多相关信息:uksort

答案 2 :(得分: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

答案 3 :(得分: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);