tags:

views:

73

answers:

5

Here is my array:

Array ( 
    [0] => Array ( [0] => content here [1] => 2010-02-04 01:25:34 )
    [1] => Array ( [0] => content here [1] => 2010-02-04 04:51:37 ) 
    [2] => Array ( [0] => content here [1] => 2010-02-04 04:52:31 ) 
    [3] => Array ( [0] => content here [1] => 2010-02-04 05:50:48 ) 
    [4] => Array ( [0] => content here [1] => 2010-02-04 03:25:34 ) 
    [5] => Array ( [0] => content here [1] => 2010-02-04 05:39:33 ) 
    [6] => Array ( [0] => content here [1] => 2010-02-04 03:25:34 ) 
    [7] => Array ( [0] => content here [1] => 2010-02-04 07:07:09 ) 
    [8] => Array ( [0] => content here [1] => 2010-02-04 07:07:23 ) 
    [9] => Array ( [0] => content here [1] => 2010-02-04 08:51:18 ) 
) 

How can I sort it by the timestamp?

A: 

Use array_multisort.

Sarfraz
A: 

How about Bubble sort?

That means looping through each date, check if the previous is bigger, etc.

Luca Matteis
+1  A: 

Use usort() with a cmp_function that compares index 1 of each of the passed arguments.

Ignacio Vazquez-Abrams
+7  A: 

Or usort() with strtotime():

function compare($e1, $e2) {
    $t1 = strtotime($e1[1]));
    $t2 = strtotime($e2[1]));

    if($t1 == t2) {
       return 0;
    }
    return ($t1 > $t2) ? 1 : -1;
}


usort($array, 'compare');
Felix Kling
In this particular case, the date strings are formatted such that a basic comparison will suffice either with comparison operators (e.g. `>`) or simply `return strcmp($e1[1], $e2[1]);`
salathe
@salathe: You are probably right. I was not sure about the string comparsion.
Felix Kling
A: 

array_multisort() It's a nasty but powerful little function. Basically you'll have to iterate through your array, pulling out the date stamp into a new array - maintaining key association. Then, sort that new array, still maintaining key association. Then throw both your newly sorted array and the original array into array_multisort() and your original array will be sorted to have keys in the same order as your sorted array.

Clear as mud? The examples on that doc page should help.

Pickle