6

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!

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for the explanation. This was perfect, except it ended up backwards for me with the Tweets ascending. I just flipped the return to return $ts2-$ts1 -- That seemed to do it.
@Jason Oops, I didn't notice that you specified descending order in your question. Yes, that's the correct way to fix it. I'll update my answer for future reference.
0

Use array_multisort.

// Obtain a list of columns
 foreach ($tweets as $key => $row) {
    $text[$key]  = $row['text'];
    $time[$key] = strtotime($row['time']);
}

array_multisort($time, SORT_DESC);

2 Comments

Don't you need $text in there somewhere too? Also, how would you re-assemble the arrays into the format he started with?
I think what you were trying to do was like the php docs example php.net/manual/en/function.array-multisort.php#example-4883 where you would need array_multisort($time, SORT_DESC, $text, SORT_ASC, $tweets);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.