Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort PHP multidimensional array by timestamp

Tags:

php

sorting

I'm currently messing around with Twitter API, but this question isn't really related to Twitter API; it's just a general PHP question.

I'm pulling a couple queries of Tweets separately and then merging the requests together. And so, basically, I'm ending up with a final array something like this at the end of it all, where the Tweets aren't necessarily in the correct chronological order:

$tweets = array(
    array(
        'text'  => 'Some tweet.',
        'time'  => 'Sat Jun 22 00:45:37 +0000 2013'
    ),
    array(
        'text'  => 'And another.',
        'time'  => 'Fri Jun 21 15:32:34 +0000 2013'
    ),
    array(
        'text'  => 'Another tweet.',
        'time'  => 'Fri Jun 21 17:24:44 +0000 2013'
    ),
    array(
        'text'  => 'And one more tweet.',
        'time'  => 'Fri Jun 21 08:01:37 +0000 2013'
    )
);

And now I'm just trying to figure out what the best way to sort this array by the timestamp of each Tweet is, in descending order.

I'm reading about usort and uasort but I'm not just quite understanding them, along with how they would correspond to using this timestamp as a comparison. So any help here would really be appreciated. Thanks in advance!

like image 541
Jason Avatar asked Jun 22 '13 02:06

Jason


People also ask

How do I sort a multidimensional array in PHP?

The array_multisort() function returns a sorted array. You can assign one or more arrays. The function sorts the first array, and the other arrays follow, then, if two or more values are the same, it sorts the next array, and so on.

How do I sort a multidimensional array by column in PHP?

Use the usort() function to sort the array. The usort() function is PHP builtin function that sorts a given array using user-defined comparison function.

How does Usort work in PHP?

The usort() function in PHP sorts a given array by using a user-defined comparison function. This function is useful in case if we want to sort the array in a new manner. This function assigns new integral keys starting from zero to the elements present in the array and the old keys are lost.


1 Answers

There's two parts to this question.

First, you need a way to compare two times - a straight string comparison of the time in the given format won't give you any useful information. You can use strtotime() to convert the strings to a unix timestamp that is just a number of seconds - allowing you to easily make comparisons.

$ts = strtotime($item['time']);

The second part is actually sorting the array. You can do that with usort() and a callback function like this:

function do_compare($item1, $item2) {
    $ts1 = strtotime($item1['time']);
    $ts2 = strtotime($item2['time']);
    return $ts2 - $ts1;
}
usort($tweets, 'do_compare');
// items in $tweets are now sorted by time

usort() works by calling your function - either named or anonymous - every time it needs to compare two elements. Your function needs to return a number less than zero if the first item comes first, zero if the items are equal, or greater than zero if the first item comes second. In your example, we're just subtracting the first timestamp from the second to get this value (if the first item is greater, the result will be negative, putting it first - a descending sort). The actual numeric value doesn't matter - just whether it's less than, equal to, or greater than zero.

Another way to do it without needing a named function is to use a closure - or an anonymous function.
That would look like this:

usort($tweets, function($item1, $item2) {
    $ts1 = strtotime($item1['time']);
    $ts2 = strtotime($item2['time']);
    return $ts2 - $ts1;
});
// items in $tweets are now sorted by time

This is essentially the same as the first solution, except we're defining the function inline instead of referring back to the name of a function that's been previously defined.

like image 71
jcsanyi Avatar answered Sep 20 '22 23:09

jcsanyi