【问题标题】:Sorting tweets by timestamp in php在php中按时间戳对推文进行排序
【发布时间】:2012-10-25 07:42:25
【问题描述】:

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

<?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

【问题讨论】:

    标签: php api sorting twitter


    【解决方案1】:

    简单! (他说)就用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
    }
    

    【讨论】:

    • 为了这是一个学校项目,第一个解决方案创造了奇迹。如此简单的修复,我不知道为什么我不知道该功能。非常感谢!
    • 嘿,数组函数一大堆,不知道所有这些我不觉得难过,你也不应该(虽然学习其他的很有趣)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多