# Thread: Comparing numerical data to array?

1. ## Comparing numerical data to array?

Ok say I have a value of 12314 And I have an array that has values of 5000, 10000, 15000, 20000, etc. I need to be able find out the difference between the original number and the next highest value in the array. I'm stumped and get the feeling I'm missing something obvious.

2. Edit:

Sorry - I'll re-post the code again later, the one I posted didn't work

-Sam

3. Edit:

next highest you say, sorry , I'll reply again

4. If you mean you want to subtract the first number in the array which is higher than the test number then you can do this:
PHP Code:
``` <?  \$arr[] = 5000;  \$arr[] = 10000;  \$arr[] = 15000;  \$arr[] = 20000;  sort (\$arr);  \$test = 12346;  for (\$i = 0, \$endLoop = sizeOf(\$arr) - 1; \$i < \$endLoop; ++\$i) {      if (\$test > \$arr[\$i]) {          continue;      } else {                   \$result = \$arr[\$i] - \$test;          break;      }  }  print \$result;  ?> ```

5. ok. here comes my reply, this takes the next highest value of the array and minuses \$num from it.
PHP Code:
``` \$arr[] = 5000; \$arr[] = 20000; \$arr[] = 10000; \$arr[] = 15000; \$num = 12345; sort(\$arr); \$result = \$arr[count(\$arr)-1] - \$num; echo('Difference between the next highest number of \$arr minus \$num is: ' .\$result); //will echo //Difference between the highest number of \$arr minus \$num is: 2655  ```

6. Originally Posted by website
ok. here comes my reply, this takes the next highest value of the array and minuses \$num from it.
PHP Code:
``` \$arr[] = 5000;\$arr[] = 20000;\$arr[] = 10000;\$arr[] = 15000;\$num = 12345;sort(\$arr);\$result = \$arr[count(\$arr)-1] - \$num;echo('Difference between the next highest number of \$arr minus \$num is: ' .\$result);//will echo//Difference between the highest number of \$arr minus \$num is: 2655  ```
that returns 7655 which is the differance between the original number and the highest number in array.

7. Originally Posted by torrent
If you mean you want to subtract the first number in the array which is higher than the test number then you can do this:
PHP Code:
``` <?\$arr[] = 5000;\$arr[] = 10000;\$arr[] = 15000;\$arr[] = 20000;sort (\$arr);\$test = 12346;for (\$i = 0, \$endLoop = sizeOf(\$arr) - 1; \$i < \$endLoop; ++\$i) {if (\$test > \$arr[\$i]) {continue;} else { \$result = \$arr[\$i] - \$test;break;}}print \$result; ?> ```
What do you know it works like a charm. Thanks alot. My head is in a quagmire this weekend. I'm still open for suggestions if someone has a better way. Best practice is what I always strive for. The above looks prety practicle though.

8. I made a slight change so that if the number was equal to a number in the array it would give the difference of the next number.

PHP Code:
``` <? \$arr[] = 5000; \$arr[] = 10000; \$arr[] = 15000; \$arr[] = 20000; sort (\$arr); \$test = 12346;  for (\$i = 0, \$endLoop = sizeOf(\$arr) - 1; \$i < \$endLoop; ++\$i) {   if (\$test > \$arr[\$i]) {    continue;   } elseif(\$test > \$arr[\$i]) {    \$result = \$arr[\$i + 1] - \$test;   } else {     \$result = \$arr[\$i] - \$test;    break;   }  }print \$result; ?> ```

9. ah fine it was solved, anyway, my head must have been broken yesterday, it should be
PHP Code:
``` \$arr[count(\$arr)-2]  ```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•