Search code examples
phparrayssorting

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.


Solution

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

    https://www.php.net/manual/en/function.usort.php

    https://www.php.net/manual/en/function.array-map.php