I have an array of numbers from descending order. When I add to this array, I add to the end and then do natsort($times). $times then looks like this (obtained by print_r):
Array
(
[0] => 0.01
[1] => 0.02
[2] => 0.05
[3] => 0.08
[7] => 0.10 <-- Just added and natsorted
[4] => 0.11
[5] => 0.14
[6] => 0.21
)
However, I wish to reassign all the keys so that the just-added 0.10 is array index 4 making it easy to see what place the new time is in. ie “your ranking is $arrayindex+1”
Besides copying this whole array into a new array to get new keys, is there a better way?
You can use
sort[docs] withSORT_NUMERIC, instead ofnatsort:Unlike
natsort, it re-indexes the array.There is no built in way to re-index the array after/while sorting.You could also usearray_values[docs] after sorting withnatsort:This is copying the array though.