cs1h Posted August 21, 2009 Share Posted August 21, 2009 Hi, Does anyone know of a way to check for numbers that fit within a certain range. The best way to explain what I mean is with an example. If I have the number 5.5 I want to be able to compare it with a group of numbers and find any numbers between 4.5 and 6.5 (i.e. plus or minus 1) in that group. Any help will be appreciated. Cs1h Quote Link to comment https://forums.phpfreaks.com/topic/171248-solved-exploring-a-range-of-numbers/ Share on other sites More sharing options...
kratsg Posted August 21, 2009 Share Posted August 21, 2009 Question: Check for numbers stored in an array, database table, string, what? Question: If they give you .5, does that mean you want to check from -.5 to 1.5 (or do you only want non-zero numbers)? Quote Link to comment https://forums.phpfreaks.com/topic/171248-solved-exploring-a-range-of-numbers/#findComment-903042 Share on other sites More sharing options...
cs1h Posted August 21, 2009 Author Share Posted August 21, 2009 The numbers will be stored in a coma separated string in a row in a database. It won't make too much difference if the numbers are -0.5 or 0.1, just which ever is easiest to do. Thanks, Cs1h Quote Link to comment https://forums.phpfreaks.com/topic/171248-solved-exploring-a-range-of-numbers/#findComment-903262 Share on other sites More sharing options...
Daniel0 Posted August 21, 2009 Share Posted August 21, 2009 Split it up using explode, create a new array, iterate through all the elements, add all the elements that satisfy the requirements to the new array. Quote Link to comment https://forums.phpfreaks.com/topic/171248-solved-exploring-a-range-of-numbers/#findComment-903266 Share on other sites More sharing options...
cs1h Posted August 21, 2009 Author Share Posted August 21, 2009 Hi, I was hoping to find a way so that if I had the list of numbers 1.8,3.9,45.2,35.1 it would be able to look through the longer string like this 11.1,15.2,1.5,4.8,45.3,35.6,77.1,12.2 With a variance of +/- 1 it would be able to identify that the pattern exists in the string 11.1,15.2,1.5,4.8,45.3,35.6,77.1,12.2 Thanks for the help so far, Cs1h Quote Link to comment https://forums.phpfreaks.com/topic/171248-solved-exploring-a-range-of-numbers/#findComment-903277 Share on other sites More sharing options...
Daniel0 Posted August 21, 2009 Share Posted August 21, 2009 Sure you can: <?php $deviance = 1; $string1 = '1.8,3.9,45.2,35.1'; $string2 = '11.1,15.2,1.5,4.8,45.3,35.6,77.1,12.2'; $array1 = explode(',', $string1); $array2 = explode(',', $string2); $matches = array(); foreach ($array1 as $item1) { $upperBound = $item1 + $deviance; $lowerBound = $item1 - $deviance; foreach ($array2 as $item2) { if ($item2 >= $lowerBound && $item2 <= $upperBound) { $matches[] = (float) $item2; } } } var_dump($matches); Output: array(4) { [0]=> float(1.5) [1]=> float(4. [2]=> float(45.3) [3]=> float(35.6) } Quote Link to comment https://forums.phpfreaks.com/topic/171248-solved-exploring-a-range-of-numbers/#findComment-903286 Share on other sites More sharing options...
cs1h Posted August 21, 2009 Author Share Posted August 21, 2009 Thank you, That is excellent. Best Regards, Cs1h Quote Link to comment https://forums.phpfreaks.com/topic/171248-solved-exploring-a-range-of-numbers/#findComment-903293 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.