SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Edit:

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


    -Sam
    Sam Hastings

  3. #3
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Edit:

    next highest you say, sorry , I'll reply again
    Last edited by website; May 31, 2003 at 10:04.
    - website

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #5
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 
    - website

  6. #6
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote 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. #7
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote 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. #8
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #9
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah fine it was solved, anyway, my head must have been broken yesterday, it should be
    PHP Code:
    $arr[count($arr)-2
    - website


Bookmarks

Posting Permissions

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