wwfc_barmy_army Posted August 14, 2009 Share Posted August 14, 2009 Hi. I have an array of a few numbers eg. 80 100 120 160 If a number is used such as 85 I need to it find 100, so the next highest number. Any ideas how i could go about this? Any ideas? thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/170221-solved-nearest-highest-number/ Share on other sites More sharing options...
Jibberish Posted August 14, 2009 Share Posted August 14, 2009 I think this should work. I havn't been able to test it though so let me know if there is any problems. <?php $numberArray = array(80,100, 120, 160); $inNumber = 85; $nearHighNum = 0; foreach($numberArray as $value) { if($value >= $inNumber && ($value <= $nearHighNum || $nearHighNum < $inNumber)) $nearHighNum = $value; } echo $nearHighNum; ?> Quote Link to comment https://forums.phpfreaks.com/topic/170221-solved-nearest-highest-number/#findComment-897930 Share on other sites More sharing options...
Bjom Posted August 14, 2009 Share Posted August 14, 2009 should work, but will continue testing values unnecessarily, when it has already found the number. $numberArray = array(80,100, 120, 160); $inNumber = 85; foreach($numberArray as $value) { if($value > $inNumber) { $nearHighNum = $value; break; } } echo $nearHighNum; Quote Link to comment https://forums.phpfreaks.com/topic/170221-solved-nearest-highest-number/#findComment-897932 Share on other sites More sharing options...
Jibberish Posted August 14, 2009 Share Posted August 14, 2009 Very true. Just a quick note though, if the array isn't hard coded and it can come through in any random order, the coder will have to make sure that the array is in the correct order first for it work, but thats not to much hastle. i.e if the array looks like this, then it will report 120 instead of100 if you pass it 85. $numberArray = array(120, 80, 100, 160); Quote Link to comment https://forums.phpfreaks.com/topic/170221-solved-nearest-highest-number/#findComment-897936 Share on other sites More sharing options...
wwfc_barmy_army Posted August 14, 2009 Author Share Posted August 14, 2009 Thanks Guys. Quote Link to comment https://forums.phpfreaks.com/topic/170221-solved-nearest-highest-number/#findComment-897940 Share on other sites More sharing options...
Adam Posted August 14, 2009 Share Posted August 14, 2009 Very true. Just a quick note though, if the array isn't hard coded and it can come through in any random order, the coder will have to make sure that the array is in the correct order first for it work, but thats not to much hastle. i.e if the array looks like this, then it will report 120 instead of100 if you pass it 85. $numberArray = array(120, 80, 100, 160); Yeah but can sort that easily with... sort($numberArray); Quote Link to comment https://forums.phpfreaks.com/topic/170221-solved-nearest-highest-number/#findComment-897944 Share on other sites More sharing options...
Daniel0 Posted August 14, 2009 Share Posted August 14, 2009 Yeah but can sort that easily with... sort($numberArray); But that will be slower. sort() uses the quicksort algorithm, so it has a complexity of O(n*log(n)). Just going through all the elements unsorted from the start is a linear search which is O(n). Quote Link to comment https://forums.phpfreaks.com/topic/170221-solved-nearest-highest-number/#findComment-898017 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.