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 Quote Link to comment 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); Quote Link to comment 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 />'; ?> Quote Link to comment 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 Quote Link to comment 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 />'; Quote Link to comment 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 />'; Quote Link to comment 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 Quote Link to comment 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.