简体   繁体   中英

usort php array/object multidimensional

I'm having trouble making php usort work.

I fetch this from 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 ) )

I'd like to order this using usort and comment_count object. I've tried this (but it is obviously wrong):

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

$sorted_by_comments_array = usort($all_events, 'sortByComments');

Error: Cannot use object of type stdClass as array

I'm finding it a little hard to read your array's structure, but I think I'm right in saying that the elements of the array are the objects you want to sort. This means that $a and $b will themselves be the objects you need to sort.

This therefore means that you don't need to access them as $a[0]->comment_count but simply as $a->comment_count :

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

(Note that the brackets are also unnecessary.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM