Schlo_50 Posted September 1, 2008 Share Posted September 1, 2008 Hello, I have a short list of numbers in a file and Im trying to find the highest number from it. The list looks like this: (short example) none|14 none|103 none|65 I'd like to, if possible, find the highest number of bkey[1] and print it out on the page. I have included my code so far below. At the moment is prints nothing but 1's. print "The largest number in the file is:<br /><br />"; $bfile = file("numbers.DAT"); foreach($bfile as $bKey => $Val){ $Data[$bKey] = explode("|", $Val); $none = $Data[$bKey][0]; $number = $Data[$bKey][1]; $arr = array($number); print ksort($arr); print "<br />"; } Thanks in advance Link to comment https://forums.phpfreaks.com/topic/122241-numerically-sorting-lists/ Share on other sites More sharing options...
Schlo_50 Posted September 1, 2008 Author Share Posted September 1, 2008 I have kept on trying and have come up with the following but the problem is that It only displays the last number of the list which isn't necessarily the largest number.. print "The largest number in the file is:<br /><br />"; $bfile = file("numbers.DAT"); foreach($bfile as $bKey => $Val){ $Data[$bKey] = explode("|", $Val); $none = $Data[$bKey][0]; $number = $Data[$bKey][1]; $arr = array($number); } print "Largest: ".max($arr); Link to comment https://forums.phpfreaks.com/topic/122241-numerically-sorting-lists/#findComment-631165 Share on other sites More sharing options...
blinky001 Posted September 1, 2008 Share Posted September 1, 2008 Try this: <?php foreach($bfile as $key => $value) { $tmp = explode('|', $value); $vals[] = $tmp[1]; } rsort($vals); echo $vals[0].'<br />'; ?> Link to comment https://forums.phpfreaks.com/topic/122241-numerically-sorting-lists/#findComment-631171 Share on other sites More sharing options...
Schlo_50 Posted September 1, 2008 Author Share Posted September 1, 2008 Thanks for the help. I tried that code and it didn't quite work after testing, although it's nearly there. I'll show you the input and output. //list of numbers none | 1 none | 2 none | 13 none | 4 none | 5 none | 6 none | 1010 none | 950 none | 93 none | 8 none | 11 none | 12 none | 3 none | 14 none | 16 //output 950 The output should infact be 1010 in this case Link to comment https://forums.phpfreaks.com/topic/122241-numerically-sorting-lists/#findComment-631179 Share on other sites More sharing options...
blinky001 Posted September 1, 2008 Share Posted September 1, 2008 Very odd, I tested it using an array and it worked fine. You may be on a different PHP version to me, so I have made two changes to the core code - give them a go. <?php print "The largest number in the file is:<br /><br />"; $bfile = array( 'none | 1', 'none | 2', 'none | 13', 'none | 4', 'none | 5', 'none | 6', 'none | 1010', 'none | 950', 'none | 93', 'none | 8', 'none | 11', 'none | 12', 'none | 3', 'none | 14', 'none | 16' ); foreach($bfile as $key => $value) { $tmp = explode('|', $value); $vals[] = trim($tmp[1]); } rsort($vals, SORT_NUMERIC); echo $vals[0].'<br />'; Link to comment https://forums.phpfreaks.com/topic/122241-numerically-sorting-lists/#findComment-631182 Share on other sites More sharing options...
Schlo_50 Posted September 1, 2008 Author Share Posted September 1, 2008 Following is not working either: function BubbleSort($sort_array,$column = 0,$reverse=0) { $lunghezza=count($sort_array); for ($i = 0; $i < $lunghezza ; $i++){ for ($j = $i + 1; $j < $lunghezza ; $j++){ if($reverse){ if ($sort_array[$i][$column] < $sort_array[$j][$column]){ $tmp = $sort_array[$i]; $sort_array[$i] = $sort_array[$j]; $sort_array[$j] = $tmp; } }else{ if ($sort_array[$i][$column] > $sort_array[$j][$column]){ $tmp = $sort_array[$i]; $sort_array[$i] = $sort_array[$j]; $sort_array[$j] = $tmp; } } } } return $sort_array; } $bfile = file("numbers.DAT"); foreach($bfile as $key => $value) { $tmp = explode('|', $value); $vals[] = $tmp[1]; } $vals = BubbleSort($vals); print $vals[1].'<br />'; Link to comment https://forums.phpfreaks.com/topic/122241-numerically-sorting-lists/#findComment-631204 Share on other sites More sharing options...
blinky001 Posted September 1, 2008 Share Posted September 1, 2008 Really odd: I get The largest number in the file is: 1010 Link to comment https://forums.phpfreaks.com/topic/122241-numerically-sorting-lists/#findComment-631211 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.