johnny44 Posted March 10, 2008 Share Posted March 10, 2008 I have three numbers, $a, $b, $c. If all three are distinct, I want to display the lowest, middle and highest. I'm using this: if( ($a==$b) || ($b==$c) || ($c==$a) ) { echo "Ignore"; } else { $U = array($a,$b,$c); sort($U); echo "Lowest $U[0], Middle $U[1], Highest $U[2]"; } Is there a more elegant way? Quote Link to comment Share on other sites More sharing options...
btherl Posted March 10, 2008 Share Posted March 10, 2008 If you have only 3 numbers, I think that's fine. If you have a large or unlimited quantity, then you might want to use a generic duplicate checking algorithm (such as putting all the numbers as keys in an associative array, checking for duplicate keys first). Quote Link to comment Share on other sites More sharing options...
Barand Posted March 10, 2008 Share Posted March 10, 2008 Here's one generic version <?php $numbers = array(10,45,1,2,3,4,5,6,20); if (array_unique($numbers) != $numbers) echo "Ignore"; else { $k = count($numbers); sort($numbers); echo 'Low : ' . $numbers[0] . '<br/>'; echo 'Middle : ' . $numbers[floor($k/2)] . '<br/>'; echo 'High : ' . $numbers[$k-1] . '<br/>'; } ?> Quote Link to comment Share on other sites More sharing options...
johnny44 Posted March 11, 2008 Author Share Posted March 11, 2008 Ah, thank you guys. array_unique looks useful. 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.