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

时间:2014-06-19 23:47:34

标签: php arrays sorting object usort

以下是示例数组$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
    )

)

我正在使用各自API(Twitter,Facebook)中的数据创建每个SocialPost Object,然后将每个SocialPost Object添加到SocialPosts数组中。

我尝试了以下rsort,以便我可以通过[time]属性以相反的顺序将它们全部列在一起:

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

rsort($socialPosts, "cmp");

然而,奇怪的是,按照正确的顺序对Twitter帖子进行排序,然后按照正确的顺序排列Facebook帖子,看起来像两个单独的排序。它应该根据[time]值以正确的顺序将它们排在一起,无论来源是什么。

在将每个SocialPost添加到数组后,我还运行一个循环来格式化然后正确更新[time]属性,因为Twitter和Facebook提供了不同格式的时间细节。这也是代码片段:

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;        
}

关于可能出现什么问题的任何想法?

2 个答案:

答案 0 :(得分:1)

rsort没有采取比较功能。要使用用户定义的比较函数进行排序,您必须使用usort

usort($socialPost, "cmp");

如果要反转排序顺序,请更改比较功能,使其反转结果。

答案 1 :(得分:1)

您应该查看usort()

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

    usort($ar, "cmp");

如果您想要撤消订单,只需将cmp功能更改为:

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

产生的回报是:

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
        )

)