我有这个包含数据的数组。我目前正在提供的foreach循环中显示它,但我想重新排序(从MAX id到MIN id)
[0] => Array (
[id] => 1
[timestamp_start_ex] => Wed Mar 9 18:28:14 2016
[timestamp_end_ex] => Wed Mar 9 19:28:14 2016
[timestamp_start] => 1457544494
[timestamp_end] => 1457548094
[orders] => 1
)
[1] => Array (
[id] => 2
[timestamp_start_ex] => Wed Mar 9 17:28:14 2016
[timestamp_end_ex] => Wed Mar 9 18:28:14 2016
[timestamp_start] => 1457540894
[timestamp_end] => 1457544494
[orders] => 0
)
[2] => Array (
[id] => 3
[timestamp_start_ex] => Wed Mar 9 16:28:14 2016
[timestamp_end_ex] => Wed Mar 9 17:28:14 2016
[timestamp_start] => 1457537294
[timestamp_end] => 1457540894
[orders] => 0
)
[3] => Array (
[id] => 4
[timestamp_start_ex] => Wed Mar 9 15:28:14 2016
[timestamp_end_ex] => Wed Mar 9 16:28:14 2016
[timestamp_start] => 1457533694
[timestamp_end] => 1457537294
[orders] => 0
)
[4] => Array (
[id] => 5
[timestamp_start_ex] => Wed Mar 9 14:28:14 2016
[timestamp_end_ex] => Wed Mar 9 15:28:14 2016
[timestamp_start] => 1457530094
[timestamp_end] => 1457533694
[orders] => 0
)
[5] => Array (
[id] => 6
[timestamp_start_ex] => Wed Mar 9 13:28:14 2016
[timestamp_end_ex] => Wed Mar 9 14:28:14 2016
[timestamp_start] => 1457526494
[timestamp_end] => 1457530094
[orders] => 0
)
[6] => Array (
[id] => 7
[timestamp_start_ex] => Wed Mar 9 12:28:14 2016
[timestamp_end_ex] => Wed Mar 9 13:28:14 2016
[timestamp_start] => 1457522894
[timestamp_end] => 1457526494
[orders] => 0
)
[7] => Array (
[id] => 8
[timestamp_start_ex] => Wed Mar 9 11:28:14 2016
[timestamp_end_ex] => Wed Mar 9 12:28:14 2016
[timestamp_start] => 1457519294
[timestamp_end] => 1457522894
[orders] => 0
)
[8] => Array (
[id] => 9
[timestamp_start_ex] => Wed Mar 9 10:28:14 2016
[timestamp_end_ex] => Wed Mar 9 11:28:14 2016
[timestamp_start] => 1457515694
[timestamp_end] => 1457519294
[orders] => 0
)
[9] => Array (
[id] => 10
[timestamp_start_ex] => Wed Mar 9 09:28:14 2016
[timestamp_end_ex] => Wed Mar 9 10:28:14 2016
[timestamp_start] => 1457512094
[timestamp_end] => 1457515694
[orders] => 0
)
[10] => Array (
[id] => 11
[timestamp_start_ex] => Wed Mar 9 08:28:14 2016
[timestamp_end_ex] => Wed Mar 9 09:28:14 2016
[timestamp_start] => 1457508494
[timestamp_end] => 1457512094
[orders] => 0
)
[11] => Array (
[id] => 12
[timestamp_start_ex] => Wed Mar 9 07:28:14 2016
[timestamp_end_ex] => Wed Mar 9 08:28:14 2016
[timestamp_start] => 1457504894
[timestamp_end] => 1457508494
[orders] => 0
)
[12] => Array (
[id] => 13
[timestamp_start_ex] => Wed Mar 9 06:28:14 2016
[timestamp_end_ex] => Wed Mar 9 07:28:14 2016
[timestamp_start] => 1457501294
[timestamp_end] => 1457504894
[orders] => 0
)
[13] => Array (
[id] => 14
[timestamp_start_ex] => Wed Mar 9 05:28:14 2016
[timestamp_end_ex] => Wed Mar 9 06:28:14 2016
[timestamp_start] => 1457497694
[timestamp_end] => 1457501294
[orders] => 0
)
[14] => Array (
[id] => 15
[timestamp_start_ex] => Wed Mar 9 04:28:14 2016
[timestamp_end_ex] => Wed Mar 9 05:28:14 2016
[timestamp_start] => 1457494094
[timestamp_end] => 1457497694
[orders] => 0
)
[15] => Array (
[id] => 16
[timestamp_start_ex] => Wed Mar 9 03:28:14 2016
[timestamp_end_ex] => Wed Mar 9 04:28:14 2016
[timestamp_start] => 1457490494
[timestamp_end] => 1457494094
[orders] => 0
)
[16] => Array (
[id] => 17
[timestamp_start_ex] => Wed Mar 9 02:28:14 2016
[timestamp_end_ex] => Wed Mar 9 03:28:14 2016
[timestamp_start] => 1457486894
[timestamp_end] => 1457490494
[orders] => 0
)
[17] => Array (
[id] => 18
[timestamp_start_ex] => Wed Mar 9 01:28:14 2016
[timestamp_end_ex] => Wed Mar 9 02:28:14 2016
[timestamp_start] => 1457483294
[timestamp_end] => 1457486894
[orders] => 0
)
[18] => Array (
[id] => 19
[timestamp_start_ex] => Wed Mar 9 00:28:14 2016
[timestamp_end_ex] => Wed Mar 9 01:28:14 2016
[timestamp_start] => 1457479694
[timestamp_end] => 1457483294
[orders] => 0
)
[19] => Array (
[id] => 20
[timestamp_start_ex] => Wed Mar 9 00:00:00 2016
[timestamp_end_ex] => Wed Mar 9 00:28:14 2016
[timestamp_start] => 1457478000
[timestamp_end] => 1457479694
[orders] => 0
)
我想反转数组,意思是:
[0] => id = 19
[1] => id = 18
(...)
[19] => id => 1
我尝试使用ksort()
但没有尝试,它不想重新组织数组。
答案 0 :(得分:2)
function cmp($item1, $item2) {
$a = intval($item1["id"]);
$b = intval($item2["id"]);
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
}
usort($array, "cmp");
答案 1 :(得分:0)
krsort()
将对键进行排序,这与id不同。您的示例显示了根据降序排序重新排序键。
最好在查询中执行ORDER BY id DESC
,但是对于这个简单的示例,因为它们已经按升序排列:
$array = array_reverse($array);
如果尚未排序,则使用id
提取array_column()
数组,并按照相同顺序对$array
进行排序时降序排序:
array_multisort(array_column($array, 'id'), SORT_DESC, $array);