5

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?

greenbandit
  • 2,267
  • 5
  • 30
  • 44

2 Answers2

4

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);
Peter Ajtai
  • 56,972
  • 13
  • 121
  • 140
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
1

As Gumbo mentioned, you should not use strcmp for integer values.

Use this function

function cmp($a, $b) {
    if ($a['time'] == $b['time'])
        return 0;
    return ($a['time'] < $b['time']) ? -1 : 1;
}
Simon
  • 22,637
  • 36
  • 92
  • 121