Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have array $leaderboard. Which contains following data

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

I tried to sort the array on basis of time. Its working fine if time values not duplicate using ksort. But for duplicated time it only shows on record from that

share|improve this question
 
possible duplicate of Sort multidimensional array by value (2) –  Jay Harris 22 hours ago
 
look into usort or the like –  gwillie 22 hours ago
 
possible duplicate of Reference: all basic ways to sort arrays and data in PHP (@JayHarris your suggested dupe is a dupe of a dupe of mine; duplicate chains are generally considered bad) –  michaelb958 22 hours ago
add comment

4 Answers

up vote 0 down vote accepted

Try this code,

<?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);
share|improve this answer
 
Thanks @Ajith S. Its working now. –  Nitin_aeon 22 hours ago
add comment

You can define your own sorting function and use uksort. More about that: uksort

share|improve this answer
add comment

give it a try please..

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');

reference http://stackoverflow.com/a/9001655/829533

share|improve this answer
 
The shorter way is to return strtotime($a['time_taken']) - strtotime($b['time_taken']) –  Im ieee 22 hours ago
 
@zzlalani Thanks –  Nitin_aeon 22 hours ago
 
@Nitin_aeon does it work for you? –  zzlalani 21 hours ago
 
It shows top 2 records wrong. Otherwise its working. –  Nitin_aeon 21 hours ago
add comment
<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>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.