1

My array:

array(n) {
[...],
[100]=>
  array(4) {
    [0]=>
    array(15) {
      ["TIME_PAIR"]=>
      string(11) "16:35"
    }
    [1]=>
    array(15) {
      ["TIME_PAIR"]=>
      string(11) "11:25"
    }
    [2]=>
    array(15) {
      ["TIME_PAIR"]=>
      string(11) "13:25"
    }
    [3]=>
    array(15) {
      ["TIME_PAIR"]=>
      string(11) "15:00"
    }
  }
[...]
}

I need to sort arrays in array(n)[100] by "TIME_PAIR" value. Som array[100][0] should be at the end. How to do such sort? Ive tried to use array_multisort but it always doesn't work for me(

ovnia
  • 2,412
  • 4
  • 33
  • 54
  • 2
    You would want to use [usort](http://us1.php.net/usort) - http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value – random_user_name Feb 26 '14 at 20:17
  • 1
    what is your desired result ? i suppose each subarray (ie. $array[100]) should be ordered by "TIME_PAIR", but how order subarrays? Could you show sample input & desired output ? – ziollek Feb 26 '14 at 20:31
  • 1
    @wingsofovnia : Try the below solution.. – Haroon Feb 26 '14 at 20:57

1 Answers1

1

Hi you can use usort PHP function to sort your array below is the complete Code.

$arr = array(
           "100"=>array(
              array("TIME_PAIR"=>"16:35"),
              array("TIME_PAIR"=>"11:25"),
              array("TIME_PAIR"=>"13:25"),
              array("TIME_PAIR"=>"15:00"),
             )

           );

echo "<pre>";
print_r($arr);
echo "</pre>";

function cmp($a, $b) {
   return $a['TIME_PAIR'] - $b['TIME_PAIR'];
}

$res = usort($arr[100],"cmp");

echo "<pre>";
print_r($arr);
echo "</pre>";

You will have the result below..

Array
(
[100] => Array
    (
        [0] => Array
            (
                [TIME_PAIR] => 11:25
            )

        [1] => Array
            (
                [TIME_PAIR] => 13:25
            )

        [2] => Array
            (
                [TIME_PAIR] => 15:00
            )

        [3] => Array
            (
                [TIME_PAIR] => 16:35
            )

    )

 )

Feel free to ask any questions... :)

Haroon
  • 480
  • 4
  • 14