2

我无法使 phpusort正常工作。

我从数据库中获取这个:

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

4

1 回答 1

1

我发现读取数组的结构有点困难,但我认为我说数组的元素是您要排序的对象是正确的。这意味着$a并且$b它们本身将是您需要排序的对象。

因此,这意味着您不需要以以下方式访问它们,$a[0]->comment_count而只需以以下方式访问它们$a->comment_count

function sortByComments($a, $b) {
    return $a->comment_count - $b->comment_count;
}

(请注意,括号也是不必要的。)

于 2012-09-16T11:41:02.130 回答