Jump to content

Find nearest number


Pig

Recommended Posts

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

 

 

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

$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?

 

 

Link to comment
Share on other sites

[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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.