YTe YTe - 5 months ago 12
PHP Question

Re-order a PHP Array

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.

Answer

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 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);