donglv6960 2016-03-09 18:35
浏览 27
已采纳

重新排序PHP数组

I have this array with data. I'm currently displaying it from my foreach loop as provided, but I want to reorder it (Starting from the MAX id to 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
    )

I would like to inverse the array, it means:

[0] => id = 19
[1] => id = 18
(...)
[19] => id => 1

I tried with ksort() but nothing, It does not want to reorganize the Array.

  • 写回答

2条回答 默认 最新

  • dsgoj7457 2016-03-09 18:38
    关注

    krsort() will sort on the keys, which is not the same as the id. Your example shows reordering the keys based on descending sort.

    It would be best to do ORDER BY id DESC in your query, but for this simple example, since they are already in ascending order:

    $array = array_reverse($array);
    

    If not already sorted, then extract an array of id using array_column() and sort that descending while sorting $array by the same order:

    array_multisort(array_column($array, 'id'), SORT_DESC, $array);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里