Pig Posted August 30, 2009 Share Posted August 30, 2009 Hi all been trying to figure out a way in PHP to find the nearest number in a group. for example, given $w=5; find the number which is closest to $w from the following [2 4 6 8 10 12 14 16 18 20]? I'm new to PHP, and was wondering if anyone had a script that can do the above. thanks Quote Link to comment https://forums.phpfreaks.com/topic/172486-find-nearest-number/ Share on other sites More sharing options...
ignace Posted August 30, 2009 Share Posted August 30, 2009 $number = 5; $nearestNumberKey = 0; $nearestDifference = PHP_INT_MAX; foreach ($numbers as $key => $value) { $difference = abs($number - $value); if ($difference < $nearestDifference || 0 === $difference) { $nearestDifference = $difference; $nearestNumberKey = $key; } } echo 'nearest number to ', $number, ' is ', $numbers[$nearestNumberKey];//Output: nearest number to 5 is 4 I realise that both 4 and 6 are candidates but as you specified key the first one he comes across wins. Quote Link to comment https://forums.phpfreaks.com/topic/172486-find-nearest-number/#findComment-909334 Share on other sites More sharing options...
Pig Posted August 30, 2009 Author Share Posted August 30, 2009 $number = 5; $nearestNumberKey = 0; $nearestDifference = PHP_INT_MAX; foreach ($numbers as $key => $value) { $difference = abs($number - $value); if ($difference < $nearestDifference || 0 === $difference) { $nearestDifference = $difference; $nearestNumberKey = $key; } } echo 'nearest number to ', $number, ' is ', $numbers[$nearestNumberKey];//Output: nearest number to 5 is 4 I realise that both 4 and 6 are candidates but as you specified key the first one he comes across wins. Thanks, but I'm after some code that will compare to numbers in a list, such as [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]. So for example, if the number is 56, the answer will be 20? Quote Link to comment https://forums.phpfreaks.com/topic/172486-find-nearest-number/#findComment-909353 Share on other sites More sharing options...
taquitosensei Posted August 30, 2009 Share Posted August 30, 2009 that's what $numbers is in his example $list="4,8,15,16,23,42"; $numbers=explode("," $list); Quote Link to comment https://forums.phpfreaks.com/topic/172486-find-nearest-number/#findComment-909385 Share on other sites More sharing options...
RussellReal Posted August 30, 2009 Share Posted August 30, 2009 [quote author=Pig link=topic=267295.msg1260742#msg1260742 Thanks, but I'm after some code that will compare to numbers in a list, such as [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]. So for example, if the number is 56, the answer will be 20? yes.. that is what you wanted is it not? and if the number is 56... and two people answered 55 and 57.. who should win.. lower or higher? Quote Link to comment https://forums.phpfreaks.com/topic/172486-find-nearest-number/#findComment-909398 Share on other sites More sharing options...
ignace Posted August 31, 2009 Share Posted August 31, 2009 Please do realise: || 0 === $difference I'm not sure if you want this at all but this selects the key that has the same value as $number if present. And yes if you pass 56, 20 would win as it creates the lowest difference and is the highest number available in the array. Sorry if this is not what you were looking for but you must understand that the answer given is in direct correlation with the question asked. Quote Link to comment https://forums.phpfreaks.com/topic/172486-find-nearest-number/#findComment-909561 Share on other sites More sharing options...
Pig Posted August 31, 2009 Author Share Posted August 31, 2009 OK. I am very confused. Can someone provide me the complete code, with 0,2,4,6,8,10,12,14,16,18,20 in the array? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/172486-find-nearest-number/#findComment-909575 Share on other sites More sharing options...
thebadbad Posted August 31, 2009 Share Posted August 31, 2009 Just specify the array before you run ignace's code: $numbers = array(0,2,4,6,8,10,12,14,16,18,20); And what you called $w is $number in his code. Quote Link to comment https://forums.phpfreaks.com/topic/172486-find-nearest-number/#findComment-909589 Share on other sites More sharing options...
Pig Posted August 31, 2009 Author Share Posted August 31, 2009 Just specify the array before you run ignace's code: $numbers = array(0,2,4,6,8,10,12,14,16,18,20); Ah ha! It works! Just what I wanted. Thanks all, very much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/172486-find-nearest-number/#findComment-909796 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.