排序多维数组 - 保留键值
Sort multi-dimensional array - keep values of keys
我想根据键(日期)对我的数组进行排序 - 最好同时使用 ASC 和 DESC。
我已经尝试在 SO 上使用几种方法,但结果总是很糟糕 - 所以我想我会再问这个问题。
我有这个数组:
Array
(
[2015-05-29] => Array
(
[a] => 13
[b] => 1
[c] => 12
)
[2015-05-28] => Array
(
[a] => 10
[b] => 1
[c] => 1
)
[2015-05-27] => Array
(
[a] => 2
)
[2015-05-30] => Array
(
[b] => 24
[c] => 25
)
)
我使用这个功能 - 根据几个帖子应该可以工作:
uasort($days, function($a, $b) {
return $a['points'] - $b['points'];
});
但事实并非如此,因为数组是这样返回的:
Array
(
[2015-05-30] => Array
(
[b] => 24
[c] => 25
)
[2015-05-27] => Array
(
[a] => 2
)
[2015-05-28] => Array
(
[a] => 10
[b] => 1
[c] => 1
)
[2015-05-29] => Array
(
[a] => 13
[b] => 1
[c] => 12
)
)
我想像这样返回数组:
Array
(
[2015-05-27] => Array
(
[a] => 2
)
[2015-05-28] => Array
(
[a] => 10
[b] => 1
[c] => 1
)
[2015-05-29] => Array
(
[a] => 13
[b] => 1
[c] => 12
)
[2015-05-30] => Array
(
[b] => 24
[c] => 25
)
)
或倒序!
谁能帮我解决这个问题?
我想根据键(日期)对我的数组进行排序 - 最好同时使用 ASC 和 DESC。
我已经尝试在 SO 上使用几种方法,但结果总是很糟糕 - 所以我想我会再问这个问题。
我有这个数组:
Array
(
[2015-05-29] => Array
(
[a] => 13
[b] => 1
[c] => 12
)
[2015-05-28] => Array
(
[a] => 10
[b] => 1
[c] => 1
)
[2015-05-27] => Array
(
[a] => 2
)
[2015-05-30] => Array
(
[b] => 24
[c] => 25
)
)
我使用这个功能 - 根据几个帖子应该可以工作:
uasort($days, function($a, $b) {
return $a['points'] - $b['points'];
});
但事实并非如此,因为数组是这样返回的:
Array
(
[2015-05-30] => Array
(
[b] => 24
[c] => 25
)
[2015-05-27] => Array
(
[a] => 2
)
[2015-05-28] => Array
(
[a] => 10
[b] => 1
[c] => 1
)
[2015-05-29] => Array
(
[a] => 13
[b] => 1
[c] => 12
)
)
我想像这样返回数组:
Array
(
[2015-05-27] => Array
(
[a] => 2
)
[2015-05-28] => Array
(
[a] => 10
[b] => 1
[c] => 1
)
[2015-05-29] => Array
(
[a] => 13
[b] => 1
[c] => 12
)
[2015-05-30] => Array
(
[b] => 24
[c] => 25
)
)
或倒序!
谁能帮我解决这个问题?