user1772738
user1772738

Reputation: 11

Sorting tweets by timestamp in php

I'm putting together a project for class that has to do with aggregating tweets in succession to create a linear, crowdsourced story. I've currently got a semi-working program, with the issue that the program displays the most recent tweet first - what I need is to sort these tweets to have them displayed by timestamp, oldest to newest. Here's the code:

<?php
$url = 'http://search.twitter.com/search.json?q=%23tweetstoryproj&lang=en&rpp=100';
$jsontwitter = file_get_contents($url);
$twitter = json_decode($jsontwitter, true);

$twittertext = $twitter["results"];
foreach($twittertext as $text){
    $text = str_replace("#tweetstoryproj", "", $text);
    echo $text['text'].'';
}

?>

Thanks a lot for your help, and if you want to contribute to the story, tweet with the hashtag: #tweetstoryproj

Upvotes: 1

Views: 373

Answers (1)

Martin Lyne
Martin Lyne

Reputation: 3065

Simple! (He says) Just use array_reverse

Add this line in before the foreach

$twittertext = array_reverse($twittertext);

Enjoy!

Alternative: If the array becomes huge it may take longer to flip it then traverse it, so you could use a "for" loop and go backwards thusly.

for($i=$count($twittertext);$i>-1;$i--) //It's late that may miss the last or the first entry, have a fiddle!
{
  // echo here
}

Upvotes: 3

Related Questions