在php中按时间戳排序推文

时间:2012-10-24 23:37:43

标签: php api sorting twitter

我正在整理一个项目,该项目与连续聚合推文有关,以创建一个线性的,众包的故事。我目前有一个半工作程序,问题是该程序首先显示最新的推文 - 我需要的是对这些推文进行排序,让它们按时间戳显示,从最旧到最新。这是代码:

<?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'].'';
}

?>

非常感谢您的帮助,如果您想为这个故事做出贡献,请发送标签:#tweetstoryproj

1 个答案:

答案 0 :(得分:3)

简单! (他说)只需使用array_reverse

在foreach

之前添加此行
$twittertext = array_reverse($twittertext);

享受!

替代: 如果数组变大,可能需要更长时间才能翻转它然后遍历它,所以你可以使用“for”循环然后倒退。

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