jamsearch Posted December 8, 2007 Share Posted December 8, 2007 Hope someone can help me. I am writing a program using php and mysql. I have ten variables with number values set in each one. None of these numbers will ever equal the same value to one another. (all will always have different values to one another) for example: $D01 = 444 $D02 = 474 $D03 = 644 $D04 = 447 $D05 = 488 $D06 = 554 $D07 = 1 $D08 = 44 $D09 = 1444 $D10 = 24 Here's the problem. I want to be able to output the variables in order from high to low. I have no problem figuring out the highest number or the lowest number but I'm baffled how to get the rest in line. I've been trying for days and nothing I come up with works. Is there a way of doing this? If I could use GOTO there would be no problem but without it ,it seems there would have to be thousands of lines of code to get the result I'm after. Any help would be greatly appreciated. Thanks. ??? Quote Link to comment Share on other sites More sharing options...
btherl Posted December 8, 2007 Share Posted December 8, 2007 Are you familiar with arrays and sorting? The simplest solution here is to put the values into an array and sort the array. For example $D[1] = 444; # ... $D[10] = 24; rsort($D); foreach ($D as $num) { print "$num\n"; } Manual for rsort() Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted December 8, 2007 Share Posted December 8, 2007 Yeah, arrays are the way to go for sorting. You could easily transform you variables to an array like: <?php error_reporting(E_ALL); $D01 = 444; $D02 = 474; $D03 = 644; $D04 = 447; $D05 = 488; $D06 = 554; $D07 = 1; $D08 = 44; $D09 = 1444; $D10 = 24; $D = array(); for($i=1;$i<=10;$i++){ $D[$i] = ${'D'.str_pad($i,2,0,STR_PAD_LEFT)}; } rsort($D); echo '<pre>'.print_r($D,1).'</pre>'; ?> Though it would be better to put them in an array to start with. Quote Link to comment Share on other sites More sharing options...
jamsearch Posted December 8, 2007 Author Share Posted December 8, 2007 Thank you so much. I am trying to teach myself PHP from books and online help but a lot of the time only a good example will help. Thank you Thank you... 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.