Scummy12 Posted February 16, 2011 Share Posted February 16, 2011 I have a txt file of numbers e.g. 1,4,3,5,19,3,11 all in a column. Is it possible to echo only the highest 3 numbers? Quote Link to comment https://forums.phpfreaks.com/topic/227853-ranking-elements-in-txt-file/ Share on other sites More sharing options...
RichardRotterdam Posted February 16, 2011 Share Posted February 16, 2011 This is how I would do it. 1. Put all values in an array. 2. Sort them by value using sort or usort 3. echo the first item. Quote Link to comment https://forums.phpfreaks.com/topic/227853-ranking-elements-in-txt-file/#findComment-1174928 Share on other sites More sharing options...
dragon_sa Posted February 16, 2011 Share Posted February 16, 2011 like this <?php $s=array('1','4','3','5','19','3','11'); rsort($s); echo $s[0]."<br/>".$s[1]."<br/>".$s[2]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/227853-ranking-elements-in-txt-file/#findComment-1174946 Share on other sites More sharing options...
Scummy12 Posted February 16, 2011 Author Share Posted February 16, 2011 like this <?php $s=array('1','4','3','5','19','3','11'); rsort($s); echo $s[0]."<br/>".$s[1]."<br/>".$s[2]; ?> Ok but the numbers are in a txt file, and lets say that there are about 30,000 numbers... Quote Link to comment https://forums.phpfreaks.com/topic/227853-ranking-elements-in-txt-file/#findComment-1174951 Share on other sites More sharing options...
dragon_sa Posted February 16, 2011 Share Posted February 16, 2011 suppose your file is called test.txt and in the same directory as the script and also just a comma seperated list of numbers $file="test.txt"; $f=fopen($file, "r"); $content=fread($f,filesize($file)); fclose($f); $del=","; $s=explode($del, $content); rsort($s); echo $s[0]."<br/>".$s[1]."<br/>".$s[2]; Quote Link to comment https://forums.phpfreaks.com/topic/227853-ranking-elements-in-txt-file/#findComment-1174960 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.