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. 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); Link to comment https://forums.phpfreaks.com/topic/287866-sorting-a-numeric-array/#findComment-1476579 Share on other sites More sharing options...
xProteuSx Posted April 18, 2014 Author 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); 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 Link to comment https://forums.phpfreaks.com/topic/287866-sorting-a-numeric-array/#findComment-1476701 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.