簡體   English   中英

usort php數組/對象多維

[英]usort php array/object multidimensional

我無法使php usort工作。

我從數據庫獲取:

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

我發現讀取數組的結構有點困難,但是我認為數組的元素就是要排序的對象是正確的。 這意味着$a$b本身就是您需要排序的對象。

因此,這意味着您不需要以$a[0]->comment_count方式訪問它們,而只需以$a->comment_count來訪問它們:

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

(請注意,括號也不必要。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM