這是我有數組:陣列關鍵是在多維數組associaltive失去排序
Array
(
[02nd Apr-04th Apr] => Array
(
[facebook] => 0
[youtube] => 9
[timestamp] => 1491177600
)
[03rd Mar-05th Mar] => Array
(
[facebook] => 0
[youtube] => 0
[timestamp] => 1488672000
)
)
我想通過「時間戳」值進行排序這一點。因此,預期輸出是:
Array
(
[03rd Mar-05th Mar] => Array
(
[facebook] => 0
[youtube] => 0
[timestamp] => 1488672000
)
[02nd Apr-04th Apr] => Array
(
[facebook] => 0
[youtube] => 9
[timestamp] => 1491177600
)
)
我做了什麼:
usort($array, function($a, $b) {
if ($a['timestamp'] == $b['timestamp']) {
return 0;
}
return ($a['timestamp'] < $b['timestamp']) ? -1 : 1;
})
當我這樣做,排序工作,但該陣列鑰匙丟失。這是我得到的結果是:
Array
(
[0] => Array
(
[facebook] => 0
[youtube] => 0
[timestamp] => 1488672000
)
[1] => Array
(
[facebook] => 0
[youtube] => 9
[timestamp] => 1491177600
)
)
這裏的東西,你可以使用http://php.net/uasort –