2

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

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 に答える