1

我一直在努力尝试通过“user_likes”和“created_time”在 PHP 中对这个数组进行排序。我在 Stackoverflow 上查看了一些示例,但无法找出正确的语法。我希望订单从最多的“user_likes”开始,然后是“created_time”,不确定它是否可能。我一直在尝试使用 array_multisort,但没有成功。

        Array
            (
                [0] => Array
                    (
                        [taskuser] => Array
                            (
                                [profilepic] => 1e605ad76c82d41f6467c2525f796ebd.jpg
                                [postid] => 598
                                [message] => sweet!
                                  [created_time] => 2012-04-23 13:53:58
                            )

                        [time_created] => 3d
                        [likes] => Array
                            (
                                [like_type] => 0
                            )

                        [comments] => Array
                            (
                                [comments] => 0
                            )

                        [user_likes] => Array
                            (
                                [user_likes] => 0
                            )

                        [user_comments] => Array
                            (
                                [user_comments] => 0
                            )

                        [user_favorite] => Array
                            (
                                [isFav] => 0
                            )

                    )

                [1] => Array
                    (
                        [taskuser] => Array
                            (
                                [profilepic] => 1e605ad76c82d41f6467c2525f796ebd.jpg
                                [postid] => 598
                                [message] => sweet!
                                  [created_time] => 2012-04-23 14:05:20
                            )

                        [time_created] => 3d
                        [likes] => Array
                            (
                                [like_type] => 0
                            )

                        [comments] => Array
                            (
                                [comments] => 0
                            )

                        [user_likes] => Array
                            (
                                [user_likes] => 0
                            )

                        [user_comments] => Array
                            (
                                [user_comments] => 0
                            )

                        [user_favorite] => Array
                            (
                                [isFav] => 0
                            )

                    )

                [2] => Array
                    (
                        [taskuser] => Array
                            (
                                [profilepic] => 1e605ad76c82d41f6467c2525f796ebd.jpg
                                [postid] => 598
                                [message] => sweet!
                                  [created_time] => 2012-04-23 14:09:11
                            )

                        [time_created] => 3d
                        [likes] => Array
                            (
                                [like_type] => 0
                            )

                        [comments] => Array
                            (
                                [comments] => 0
                            )

                        [user_likes] => Array
                            (
                                [user_likes] => 0
                            )

                        [user_comments] => Array
                            (
                                [user_comments] => 0
                            )

                        [user_favorite] => Array
                            (
                                [isFav] => 0
                            )

                    )

                [3] => Array
                    (
                        [taskuser] => Array
                            (
                                [profilepic] => 1e605ad76c82d41f6467c2525f796ebd.jpg
                                [postid] => 598
                                [message] => sweet!
                                  [created_time] => 2012-04-23 14:11:35
                            )

                        [time_created] => 3d
                        [likes] => Array
                            (
                                [like_type] => 0
                            )

                        [comments] => Array
                            (
                                [comments] => 0
                            )

                        [user_likes] => Array
                            (
                                [user_likes] => 0
                            )

                        [user_comments] => Array
                            (
                                [user_comments] => 0
                            )

                        [user_favorite] => Array
                            (
                                [isFav] => 0
                            )

                    )

                [4] => Array
                    (
                        [taskuser] => Array
                            (
                                [profilepic] => 1e605ad76c82d41f6467c2525f796ebd.jpg
                                [postid] => 598
                                [message] => sweet!
                                  [created_time] => 2012-05-25 11:22:17
                            )

                        [time_created] => 3d
                        [likes] => Array
                            (
                                [like_type] => 3
                            )

                        [comments] => Array
                            (
                                [comments] => 1
                            )

                        [user_likes] => Array
                            (
                                [user_likes] => 0
                            )

                        [user_comments] => Array
                            (
                                [user_comments] => 0
                            )

                        [user_favorite] => Array
                            (
                                [isFav] => 0
                            )

                    )

                [5] => Array
                    (
                        [taskuser] => Array
                            (
                                [profilepic] => 1e605ad76c82d41f6467c2525f796ebd.jpg
                                [postid] => 598
                                [message] => sweet!
                                  [created_time] => 2012-04-17 15:56:00
                            )

                        [time_created] => 3d
                        [likes] => Array
                            (
                                [like_type] => 0
                            )

                        [comments] => Array
                            (
                                [comments] => 0
                            )

                        [user_likes] => Array
                            (
                                [user_likes] => 0
                            )

                        [user_comments] => Array
                            (
                                [user_comments] => 0
                            )

                        [user_favorite] => Array
                            (
                                [isFav] => 0
                            )

                    )

                [6] => Array
                    (
                        [taskuser] => Array
                            (
                                [profilepic] => 1e605ad76c82d41f6467c2525f796ebd.jpg
                                [postid] => 598
                                [message] => sweet!
                                  [created_time] => 2012-04-17 15:58:03
                            )

                        [time_created] => 3d
                        [likes] => Array
                            (
                                [like_type] => 0
                            )

                        [comments] => Array
                            (
                                [comments] => 0
                            )

                        [user_likes] => Array
                            (
                                [user_likes] => 0
                            )

                        [user_comments] => Array
                            (
                                [user_comments] => 0
                            )

                        [user_favorite] => Array
                            (
                                [isFav] => 0
                            )

                    )

                [7] => Array
                    (
                        [taskuser] => Array
                            (
                                [profilepic] => 1e605ad76c82d41f6467c2525f796ebd.jpg
                                [postid] => 598
                                [message] => sweet!
                                  [created_time] => 2012-04-20 16:24:53
                            )

                        [time_created] => 3d
                        [likes] => Array
                            (
                                [like_type] => 0
                            )

                        [comments] => Array
                            (
                                [comments] => 0
                            )

                        [user_likes] => Array
                            (
                                [user_likes] => 0
                            )

                        [user_comments] => Array
                            (
                                [user_comments] => 0
                            )

                        [user_favorite] => Array
                            (
                                [isFav] => 0
                            )

                    )

                [8] => Array
                    (
                        [taskuser] => Array
                            (
                                [profilepic] => 1e605ad76c82d41f6467c2525f796ebd.jpg
                                [postid] => 598
                                [message] => sweet!
                                  [created_time] => 2012-05-11 15:56:55
                            )

                        [time_created] => 3d
                        [likes] => Array
                            (
                                [like_type] => 1
                            )

                        [comments] => Array
                            (
                                [comments] => 0
                            )

                        [user_likes] => Array
                            (
                                [user_likes] => 0
                            )

                        [user_comments] => Array
                            (
                                [user_comments] => 0
                            )

                        [user_favorite] => Array
                            (
                                [isFav] => 0
                            )

                    )

                [9] => Array
                    (
                        [taskuser] => Array
                            (
                               [profilepic] => 1e605ad76c82d41f6467c2525f796ebd.jpg
                                [postid] => 598
                                [message] => sweet!
                                  [created_time] => 2012-05-11 16:05:35
                            )

                        [time_created] => 3d
                        [likes] => Array
                            (
                                [like_type] => 1
                            )

                        [comments] => Array
                            (
                                [comments] => 4
                            )

                        [user_likes] => Array
                            (
                                [user_likes] => 0
                            )

                        [user_comments] => Array
                            (
                                [user_comments] => 0
                            )

                        [user_favorite] => Array
                            (
                                [isFav] => 0
                            )

                    )

                [10] => Array
                    (
                        [taskuser] => Array
                            (
                               [profilepic] => 1e605ad76c82d41f6467c2525f796ebd.jpg
                                [postid] => 598
                                [message] => sweet!
                                  [created_time] => 2012-05-30 21:04:00
                            )

                        [time_created] => 3d
                        [likes] => Array
                            (
                                [like_type] => 0
                            )

                        [comments] => Array
                            (
                                [comments] => 0
                            )

                        [user_likes] => Array
                            (
                                [user_likes] => 0
                            )

                        [user_comments] => Array
                            (
                                [user_comments] => 0
                            )

                        [user_favorite] => Array
                            (
                                [isFav] => 0
                            )

                    )

                [11] => Array
                    (
                        [taskuser] => Array
                            (
                               [profilepic] => 1e605ad76c82d41f6467c2525f796ebd.jpg
                                [postid] => 598
                                [message] => sweet!
                                  [created_time] => 2012-05-30 21:04:55
                            )

                        [time_created] => 3d
                        [likes] => Array
                            (
                                [like_type] => 0
                            )

                        [comments] => Array
                            (
                                [comments] => 0
                            )

                        [user_likes] => Array
                            (
                                [user_likes] => 0
                            )

                        [user_comments] => Array
                            (
                                [user_comments] => 0
                            )

                        [user_favorite] => Array
                            (
                                [isFav] => 0
                            )

                    )

                [12] => Array
                    (
                        [taskuser] => Array
                            (
                                [profilepic] => 1e605ad76c82d41f6467c2525f796ebd.jpg
                                [postid] => 598
                                [message] => sweet!
                                  [created_time] => 2012-04-17 15:54:32
                            )

                        [time_created] => 3d
                        [likes] => Array
                            (
                                [like_type] => 0
                            )

                        [comments] => Array
                            (
                                [comments] => 0
                            )

                        [user_likes] => Array
                            (
                                [user_likes] => 0
                            )

                        [user_comments] => Array
                            (
                                [user_comments] => 0
                            )

                        [user_favorite] => Array
                            (
                                [isFav] => 0
                            )

                    )

                [13] => Array
                    (
                        [taskuser] => Array
                            (
                                [profilepic] => 1e605ad76c82d41f6467c2525f796ebd.jpg
                                [postid] => 598
                                [message] => sweet!
                                  [created_time] => 2012-04-17 15:54:54
                            )

                        [time_created] => 3d
                        [likes] => Array
                            (
                                [like_type] => 0
                            )

                        [comments] => Array
                            (
                                [comments] => 0
                            )

                        [user_likes] => Array
                            (
                                [user_likes] => 0
                            )

                        [user_comments] => Array
                            (
                                [user_comments] => 0
                            )

                        [user_favorite] => Array
                            (
                                [isFav] => 0
                            )

                    )

                [14] => Array
                    (
                        [taskuser] => Array
                            (
                                [profilepic] => 1e605ad76c82d41f6467c2525f796ebd.jpg
                                [postid] => 598
                                [message] => sweet!
                                  [created_time] => 2012-04-17 15:53:19
                            )

                        [time_created] => 3d
                        [likes] => Array
                            (
                                [like_type] => 0
                            )

                        [comments] => Array
                            (
                                [comments] => 1
                            )

                        [user_likes] => Array
                            (
                                [user_likes] => 0
                            )

                        [user_comments] => Array
                            (
                                [user_comments] => 0
                            )

                        [user_favorite] => Array
                            (
                                [isFav] => 0
                            )

                    )

            )
4

2 回答 2

1

你想要usort。让回调比较user_likes,然后比较created_time

于 2012-07-12T23:44:54.413 回答
1

其中 $results 是上面的数组,请尝试

usort($results, function($a, $b){
    //check case for same number of user likes
    if ($a['user_likes']['user_likes'] == $b['user_likes']['user_likes']){
        if (strtotime($a['taskuser']['created_time']) == strtotime($b['taskuser']['created_time'])){
            return 0;
        } else if (strtotime($a['taskuser']['created_time']) < strtotime($b['taskuser']['created_time'])){
            return 1;
        } else {
            return -1;
        }

    } else if ($a['user_likes']['user_likes'] < $b['user_likes']['user_likes']){
        return 1;
    } else {
        return -1;  
    }
});
于 2012-07-13T01:15:35.187 回答