我无法让php usort
工作。
我从DB获取此信息:
Array ( [0] => stdClass Object ( [ID] => 10 [post_author] => 1 [post_date] => 2012-09-14 15:09:33 [post_date_gmt] => 2012-09-14 15:09:33 [post_content] => asdad [post_title] => geo [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => closed [post_password] => [post_name] => geo [to_ping] => [pinged] => [post_modified] => 2012-09-16 10:54:14 [post_modified_gmt] => 2012-09-16 10:54:14 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/gold/?post_type=tribe_events&p=10 [menu_order] => 0 [post_type] => tribe_events [post_mime_type] => [comment_count] => 0 [EventStartDate] => 2012-09-19 00:00:00 [EventEndDate] => 2012-09-19 23:59:59 [filter] => raw ) [1] => stdClass Object ( [ID] => 39 [post_author] => 1 [post_date] => 2012-09-16 11:01:32 [post_date_gmt] => 2012-09-16 11:01:32 [post_content] =>
Sorry, this entry is only available in English and Russian.
[post_title] => 19 [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => 39 [to_ping] => [pinged] => [post_modified] => 2012-09-16 11:01:49 [post_modified_gmt] => 2012-09-16 11:01:49 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/gold/?post_type=tribe_events&p=39 [menu_order] => 0 [post_type] => tribe_events [post_mime_type] => [comment_count] => 0 [EventStartDate] => 2012-09-19 08:00:00 [EventEndDate] => 2012-09-19 17:00:00 [filter] => raw ) [2] => stdClass Object ( [ID] => 37 [post_author] => 1 [post_date] => 2012-09-16 10:55:47 [post_date_gmt] => 2012-09-16 10:55:47 [post_content] => asd [post_title] => event [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => event [to_ping] => [pinged] => [post_modified] => 2012-09-16 10:55:47 [post_modified_gmt] => 2012-09-16 10:55:47 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/gold/?post_type=tribe_events&p=37 [menu_order] => 0 [post_type] => tribe_events [post_mime_type] => [comment_count] => 1 [EventStartDate] => 2012-09-21 08:00:00 [EventEndDate] => 2012-09-21 17:00:00 [filter] => raw ) [3] => stdClass Object ( [ID] => 38 [post_author] => 1 [post_date] => 2012-09-16 10:57:01 [post_date_gmt] => 2012-09-16 10:57:01 [post_content] => asdsa [post_title] => sada [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => sada [to_ping] => [pinged] => [post_modified] => 2012-09-16 10:57:01 [post_modified_gmt] => 2012-09-16 10:57:01 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/gold/?post_type=tribe_events&p=38 [menu_order] => 0 [post_type] => tribe_events [post_mime_type] => [comment_count] => 0 [EventStartDate] => 2012-09-27 08:00:00 [EventEndDate] => 2012-09-30 17:00:00 [filter] => raw ) [4] => stdClass Object ( [ID] => 38 [post_author] => 1 [post_date] => 2012-09-16 10:57:01 [post_date_gmt] => 2012-09-16 10:57:01 [post_content] => asdsa [post_title] => sada [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => sada [to_ping] => [pinged] => [post_modified] => 2012-09-16 10:57:01 [post_modified_gmt] => 2012-09-16 10:57:01 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/gold/?post_type=tribe_events&p=38 [menu_order] => 0 [post_type] => tribe_events [post_mime_type] => [comment_count] => 0 [EventStartDate] => 2012-09-28 08:00:00 [EventEndDate] => 2012-10-01 17:00:00 [filter] => raw ) [5] => stdClass Object ( [ID] => 38 [post_author] => 1 [post_date] => 2012-09-16 10:57:01 [post_date_gmt] => 2012-09-16 10:57:01 [post_content] => asdsa [post_title] => sada [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => sada [to_ping] => [pinged] => [post_modified] => 2012-09-16 10:57:01 [post_modified_gmt] => 2012-09-16 10:57:01 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/gold/?post_type=tribe_events&p=38 [menu_order] => 0 [post_type] => tribe_events [post_mime_type] => [comment_count] => 0 [EventStartDate] => 2012-09-29 08:00:00 [EventEndDate] => 2012-10-02 17:00:00 [filter] => raw ) [6] => stdClass Object ( [ID] => 38 [post_author] => 1 [post_date] => 2012-09-16 10:57:01 [post_date_gmt] => 2012-09-16 10:57:01 [post_content] => asdsa [post_title] => sada [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => sada [to_ping] => [pinged] => [post_modified] => 2012-09-16 10:57:01 [post_modified_gmt] => 2012-09-16 10:57:01 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/gold/?post_type=tribe_events&p=38 [menu_order] => 0 [post_type] => tribe_events [post_mime_type] => [comment_count] => 0 [EventStartDate] => 2012-09-30 08:00:00 [EventEndDate] => 2012-10-03 17:00:00 [filter] => raw ) )
我想使用usort和comment_count对象来订购它。我试过这个(但显然是错的):
function sortByComments($a, $b) {
return ($a[0]->comment_count) - ($b[0]->comment_count);
}
$sorted_by_comments_array = usort($all_events, 'sortByComments');
错误:Cannot use object of type stdClass as array
答案 0 :(得分:1)
我发现读取数组的结构有点难,但我认为我说的是数组的元素是你想要排序的对象。这意味着$a
和$b
本身就是您需要排序的对象。
因此,这意味着您无需像$a[0]->comment_count
那样访问它们,而只需$a->comment_count
:
function sortByComments($a, $b) {
return $a->comment_count - $b->comment_count;
}
(注意括号也是不必要的。)