LanceT Posted January 31, 2007 Share Posted January 31, 2007 Alright so I'm using PHP to generate a number of scores.After the score is calculated (my script does this), I have a variable called "score." I'm generating several of these scores and I want to sort them from highest to lowest and then print them out highest to lowest. I'm guessing I would use a PHP array and then use the sort method.Anyone have some syntax of how I would do this? Link to comment https://forums.phpfreaks.com/topic/36418-using-an-array-to-sort-a-list-of-numbers/ Share on other sites More sharing options...
Psycho Posted January 31, 2007 Share Posted January 31, 2007 rsort($array);http://us2.php.net/manual/en/function.rsort.php Link to comment https://forums.phpfreaks.com/topic/36418-using-an-array-to-sort-a-list-of-numbers/#findComment-173248 Share on other sites More sharing options...
LanceT Posted January 31, 2007 Author Share Posted January 31, 2007 I also need some help on declaring an array and then inserting each value into the aray. Thanks. Link to comment https://forums.phpfreaks.com/topic/36418-using-an-array-to-sort-a-list-of-numbers/#findComment-173254 Share on other sites More sharing options...
LanceT Posted January 31, 2007 Author Share Posted January 31, 2007 looking some more it looks like I can just do this right$Array[]=$score;Will it add on to the end of the array when i do$Array[]=$score;again?Also which way of rsort would I need to sort them in numerical value order? Link to comment https://forums.phpfreaks.com/topic/36418-using-an-array-to-sort-a-list-of-numbers/#findComment-173260 Share on other sites More sharing options...
boo_lolly Posted January 31, 2007 Share Posted January 31, 2007 see the manual. [url=http://www.php.net/manual/en/language.types.array.php]array()[/url] and [url=http://php.net/rsort]rsort()[/url]. Link to comment https://forums.phpfreaks.com/topic/36418-using-an-array-to-sort-a-list-of-numbers/#findComment-173263 Share on other sites More sharing options...
Psycho Posted January 31, 2007 Share Posted January 31, 2007 sort() will sort ascending, rsort will sort descending, which is apparently what you want. And, yes, you can just use $Array[] = $score to have the $score added to the array as a new item.[code]<?php$scores = 12;$scores = 8;$scores = 3;$scores = 22;$scores = 19;$scores = 5;rsort($scores);print_r(scores);?>[/code] Link to comment https://forums.phpfreaks.com/topic/36418-using-an-array-to-sort-a-list-of-numbers/#findComment-173358 Share on other sites More sharing options...
JasonLewis Posted January 31, 2007 Share Posted January 31, 2007 mjdamato you missed the [] on your $scores variable.[code]<?php$scores[] = 12;$scores[] = 8;$scores[] = 3;$scores[] = 22;$scores[] = 19;$scores[] = 5;rsort($scores);print_r($scores);?>[/code] Link to comment https://forums.phpfreaks.com/topic/36418-using-an-array-to-sort-a-list-of-numbers/#findComment-173511 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.