xProteuSx Posted April 18, 2014 Share Posted April 18, 2014 I have an array that looks like this: Array ([0] => 1 [3] => [4] => 285 [6] => 190) and I would like to sort it from highest value to lowest. As is, when I print_r() this array, I get: 1 104 285 190 I would like to get: 1 104 190 285 I have tried the following: sort(), asort(), ksort(), and krsort(). What else can I try? My code looks like this: $countries_array = array_unique($countries_array); print_r($countries_array); which returns: 1 104 285 190 However, when I do this: $countries_array = array_unique($countries_array); $countries_array = sort($countries_array); print_r($countries_array); It simply returns: 1 Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/287866-sorting-a-numeric-array/ Share on other sites More sharing options...
Ch0cu3r Posted April 18, 2014 Share Posted April 18, 2014 You just call sort, you dont assign sort to a variable $countries_array = array_unique($countries_array); sort($countries_array); print_r($countries_array); Quote Link to comment https://forums.phpfreaks.com/topic/287866-sorting-a-numeric-array/#findComment-1476579 Share on other sites More sharing options...
Solution xProteuSx Posted April 18, 2014 Author Solution Share Posted April 18, 2014 And as is customary, I figure this all out about 2 seconds after I post for help. The trick is that you don't need this: $countries_array = sort($countries_array); You simply do this: sort($countries_array); Quote Link to comment https://forums.phpfreaks.com/topic/287866-sorting-a-numeric-array/#findComment-1476580 Share on other sites More sharing options...
QuickOldCar Posted April 19, 2014 Share Posted April 19, 2014 If you wanted a natural sorting of numbers you would use natsort() otherwise numbers group by similar digits and not how say a normal person counts 1 11 111 2 22 222 Quote Link to comment https://forums.phpfreaks.com/topic/287866-sorting-a-numeric-array/#findComment-1476701 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.