我有一個存儲在名爲$ restrictedEvents的變量中的eventbrite.com api返回的數據數組,其格式如下所示。這僅僅是一個用於粘貼的事件的代表,但它在整個數組中有一個像這樣的大約80個stdClass對象。如何通過每個對象的屬性對對象數組進行排序
我想在每個stdClass對象中按[title]鍵按字母順序排列此數組。我已經嘗試使用:
usort($restrictedEvents, "title");
然而,這將返回以下錯誤:
Warning: usort() [function.usort]: Invalid comparison function in model.php on line 109
我的猜測是它無法找到標題關鍵,因爲這是一個新的水平下降。任何指向哪裏我錯了什麼,以及如何我可以按標題排序將不勝感激。非常感謝。
Array
(
[4791063199] => stdClass Object
(
[box_header_text_color] => 393837
[link_color] => EE6600
[box_background_color] => FFFFFF
[box_border_color] => D9D4D0
[timezone] => Europe/London
[organizer] => stdClass Object
(
[url] => http://www.eventbrite.com/org/2866607767
[description] =>
[long_description] =>
[id] => 2866607767
[name] => B&Q Manifestival
)
[background_color] => E3DFDC
[id] => 4791063199
[category] =>
[box_header_background_color] => F0ECE9
[capacity] => 20
[num_attendee_rows] => 0
[title] => Closed Event Test
[start_date] => 2012-11-07 19:00:00
[status] => Live
[description] => Lorem ipsum
[end_date] => 2012-11-07 21:00:00
[tags] =>
[timezone_offset] => GMT+0000
[text_color] => 393837
[title_text_color] =>
[password] =>
[tickets] => Array
(
[0] => stdClass Object
(
[ticket] => stdClass Object
(
[description] =>
[end_date] => 2012-11-07 17:00:00
[min] => 1
[max] => 1
[price] => 0.00
[quantity_sold] => 0
[visible] => true
[currency] => GBP
[quantity_available] => 20
[type] => 0
[id] => 15940001
[name] => Manifestival Event
)
)
)
[created] => 2012-11-07 10:40:36
[url] => http://www.eventbrite.com/event/4791063199
[box_text_color] => 393837
[privacy] => Private
[venue] => stdClass Object
(
[city] =>
[name] => HR Training Room
[country] =>
[region] =>
[longitude] => 0
[postal_code] =>
[address_2] =>
[address] =>
[latitude] => 0
[country_code] =>
[id] => 2619469
[Lat-Long] => 0.0/0.0
)
[modified] => 2012-11-07 10:47:20
[repeats] => no
)
因爲他處理的是stdclass而不是數組,所以它需要返回strcmp($ a-> title,$ b-> title);',但除此之外它應該可以工作。 – Beardy
@Beardy謝謝你指出。剛更新了答案。 –
非常感謝你回答這個問題! –