douyong4842 2014-06-19 23:47
浏览 45
已采纳

使用对象的值对对象数组进行排序

Here's the example array, $SocialPosts:

Array(

[18] => SocialPost Object
    (
        [time] => 20140415
        [url] => http://www.twitter.com/twitterapi
        [copy] => We have agreed to acquire @gnip, welcome to the flock! https://t.co/fXrE36fjPZ
        [image] => 
    )

[19] => SocialPost Object
    (
        [time] => 20140409
        [url] => http://www.twitter.com/twitterapi
        [copy] => RT @twittersecurity: http://t.co/OOBCosuKND & http://t.co/oPmJvpbS6v servers were not affected by OpenSSL vulnerability CVE-2014-0160 http:…
        [image] => 
    )

[20] => SocialPost Object
    (
        [time] => 20140602
        [url] => http://www.facebook.com/19292868552
        [copy] => Our June 2014 events calendar is up! Join us at events around the world this month, and learn how to build, grow, and monetize your apps with Facebook: https://developers.facebook.com/blog/post/2014/06/02/june-2014-developer-events-for-facebook-and-parse/
        [image] => https://fbexternal-a.akamaihd.net/safe_image.php?d=AQBMV0YW8BCmCBMB&w=154&h=154&url=https%3A%2F%2Ffbstatic-a.akamaihd.net%2Frsrc.php%2Fv2%2Fy6%2Fr%2FYQEGe6GxI_M.png
    )

)

I'm creating each SocialPost Object by using data from their respective APIs (Twitter, Facebook), and then adding each SocialPost Object to a SocialPosts array.

I tried the following rsort so that I could list them all together in reverse order by the [time] property:

function cmp($a, $b){
    return strcmp($a->time, $b->time);
}

rsort($socialPosts, "cmp");

However, it's oddly enough sorting the Twitter posts in the right order, followed by the Facebook posts in the right order, in what looks like two separate sorts. It should order them together regardless source in the correct order according to the [time] value.

It's also worth nothing that after adding each SocialPost to the array, I run a loop to format and then update the [time] property correctly, since Twitter and Facebook provided the time detail in different formats. Here's that snippet as well:

foreach($socialPosts as $socialPost){
    $date_string = $socialPost->time;

    $date = new DateTime($date_string);
    $date->setTimezone(new DateTimeZone('America/New_York'));
    $formatted_date = $date->format('Ymd');

    $socialPost->time = $formatted_date;        
}

Any ideas as to what could be going wrong?

  • 写回答

2条回答 默认 最新

  • dongmu1996 2014-06-20 00:06
    关注

    You should look into usort()

        function cmp($a, $b) {
            return strtotime($a->time) - strtotime($b->time);
        }
    
        usort($ar, "cmp");
    

    if you want to reverse the orders, just change the cmp function to:

    return strtotime($b->time) - strtotime($a->time);
    

    Which yields a return of:

    Array
    (
        [0] => stdClass Object
            (
                [time] => 20140409
                [url] => http://www.twitter.com/twitterapi
                [copy] => RT @twittersecurity: http://t.co/OOBCosuKND & http://t.co/oPmJvpbS6v servers were not affected by OpenSSL vulnerability CVE-2014-0160 http:…
                [image] => 
            )
    
        [1] => stdClass Object
            (
                [time] => 20140415
                [url] => http://www.twitter.com/twitterapi
                [copy] => We have agreed to acquire @gnip, welcome to the flock! https://t.co/fXrE36fjPZ
                [image] => 
            )
    
        [2] => stdClass Object
            (
                [time] => 20140602
                [url] => http://www.facebook.com/19292868552
                [copy] => Our June 2014 events calendar is up! Join us at events around the world this month, and learn how to build, grow, and monetize your apps with Facebook: https://developers.facebook.com/blog/post/2014/06/02/june-2014-developer-events-for-facebook-and-parse/
                [image] => https://fbexternal-a.akamaihd.net/safe_image.php?d=AQBMV0YW8BCmCBMB&w=154&h=154&url=https%3A%2F%2Ffbstatic-a.akamaihd.net%2Frsrc.php%2Fv2%2Fy6%2Fr%2FYQEGe6GxI_M.png
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程