我对这个数组排序有问题:
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
)
)
它存储在变量中:
$unsorted_users
我想通过registration_date订购这个数组DESC ,所以它看起来像这样:
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 代码错误?