Search code examples
phparrayssortingmultidimensional-array

Sort rows of a 2d array by column value


I have an array like this:

Array
(
    [0] => Array
        (
            [title] => some title
            [time] => 1279231500
        )

    [1] => Array
        (
            [title] => some title 2
            [time] => 1279231440
        )
    
    [2] => Array
        (
            [title] => some title 3
            [time] => 1279229880
        )
)

How I can sort it based on time?


Solution

  • You can sort it this way (since it is an associative array):

    function cmp($a, $b)
    {
       return strcmp($a['time'], $b['time']);
    }
    
    usort($your_array, "cmp");
    print_r($your_array);