我一直在努力尝试通过“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
)
)
)
答案 0 :(得分:1)
你想要usort
。将回调与user_likes
进行比较,然后与created_time
进行比较。
答案 1 :(得分: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;
}
});