我有問題,排序此陣:由REGISTRATION_DATE我usort代碼都不盡如人意
$unsorted_users
我想訂這種陣列DESC:
Array
(
[0] => stdClass Object
(
[id] => 120
[registration_date] => 2012-10-19 16:57:46
[username] => Jeff
)
[1] => stdClass Object
(
[id] => 121
[registration_date] => 2012-12-23 16:57:46
[username] => Peter
)
[2] => stdClass Object
(
[id] => 122
[registration_date] => 2012-11-30 16:57:46
[username] => Susan
)
)
它存儲在變量所以它看起來像這樣:
Array
(
[0] => stdClass Object
(
[id] => 121
[registration_date] => 2012-12-23 16:57:46
[username] => Peter
)
[1] => stdClass Object
(
[id] => 122
[registration_date] => 2012-11-30 16:57:46
[username] => Susan
)
[2] => stdClass Object
(
[id] => 120
[registration_date] => 2012-10-19 16:57:46
[username] => Jeff
)
)
它是由REGISTRATION_DATE像下令:
2012-12-23 16:57:46
2012-11-30 16:57:46
2012-10-19 16:57:46
代替原來它是這樣的:
2012-10-19 16:57:46
2012-12-23 16:57:46
2012-11-30 16:57:46
我使用這個代碼,但它不工作良好(的print_r($ sorted_users) ; $ sorted_users的輸出是「1」。我不是爲什麼它是1而不是排序陣列。)
$sorted_users = usort($unsorted_users, function($a, $b) {
return $a['registration_date'] - $b['registration_date'];
});
任何意見爲什麼我的代碼爲$ sorted_users錯誤?
我得到這個錯誤:致命錯誤:無法使用類型stdClass的對象作爲數組 – Derfder
是,正如@jereon所說,需要登陸 –
好的,解決了,謝謝你們。 – Derfder