Question

I've taken a look at a few googles and actually found this stack too: How to sort a date array in PHP

I'ts on the same wave length but i'm not sure i get it clearly. I've read over the phpnet documentation too... As I said, i'm feeling shakey from it.

I have an array:

Array
(
    [0] => Array
        (
            [created] => 2012-06-06 21:26:25
        )

    [1] => Array
        (
            [created] => 2012-06-06 21:23:45
        )

)

And I basically need to sort this array so that they are in date order. How does this call back function work for usort? Any examples would be great!

Was it helpful?

Solution

function MySort($a, $b)
{
    if ($a['created'] == $b['created']) return 0;
    return $a['created'] < $b['created'] ? -1 : 1;
}

then use...

usort($myarray, "MySort");

OTHER TIPS

usort just lets you sort using your own criteria. You can just simply do this:

usort($array, function($a, $b){
    $a = strtotime($a['created']);
    $b = strtotime($b['created']);
    return $a-$b;
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top