Skip to content
Advertisement

PHP Sorting Inner, Inner Array

Okay I have an array like this:

array {
    [0] => array {
               [0] => array {
                         ["Time"] => "01:00:00"
                      }
               [1] => array {
                         ["Time"] => "00:00:00"
                      }
    }
    [1] => array {
               [0] => array {
                         ["Time"] => "01:00:00"
                      }
               [1] => array {
                         ["Time"] => "00:00:00"
                      }
    }
}

Now what I want to do is sort the inner inner array (the one with the time values) by the time values. Just so that inner array is sorted by the time values. How would I do this?

I do have PHP 5.3 installed if that is helpful.

Advertisement

Answer

function custom_map( $array ) {
    usort( $array, function( $a, $b ) {
        return strtotime($a['Time'])-strtotime($b['Time']) > 0;
    });
    return $array;
}

$new_array = array_map( "custom_map", $array );

http://se1.php.net/manual/en/function.usort.php

http://se1.php.net/manual/en/function.array-map.php

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement