Php script help..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 4Pics
    Confirmed User
    • Dec 2001
    • 7952

    #1

    Php script help..

    How do I take out the bottom x #'s and top x #'s?

    What I want is a better average than

    10.00 + 20.00 + 25.00 + 10.00 = 65.00 / 4 = 16.25
    the way I'd like is
    20.00 + 25.00 = 45.00 / 2 = 22.50
    since I thru out the top and bottom $ I could then multiple
    by 30 and get a closer guess as to my month income.

    But.. As the month goes on I'd want a variable set so as it gets more days it'd throw out maybe 2,3,4 days of top and bottom to find the average?

    Hopefully that makes sense...
  • StuartD
    Sofa King Band
    • Jul 2002
    • 29903

    #2
    You could put all your numbers into an array..... sort it with array_sort() and then when you do your addition, just leave out the first and last....

    when you loop over the array to do the addition, just leave out the first and last numbers....

    PHP Code:
    for($i=1; $i<count(numbers)-2;$i++){
     $total = $total + numbers[$i];
    }
    
    echo "my total is ".$total; 
    
    or something like that.
    This is me on facebook
    This is me on twitter

    Comment

    • Nathan
      Confirmed User
      • Jul 2003
      • 3108

      #3
      Hey 4Pics..

      do this:

      1) Put all numbers for the days of month in an array $days
      2) calc the number of top/bottom numbers to cut out with something like

      $cut = (date('d') < 3)?0:ceil(date('d')/10);

      that would give you 0 cut offs on day 1 and 2, 1 cut offs from day 3 to 10, 2 cut offs from day 11 to 20 and 3 cut offs from 21 to 30 and 4 on 31.

      This might be too high, so you would just have to change the /10 to something higher to make the cut offs go down.

      3) run sort($days, SORT_NUMERIC);

      4) then run this:
      while($cut--) {
      array_pop($days);
      array_shift($days);
      }

      5) calc the average: $avg = array_sum($days) / count($days);
      "Think about it a little more and you'll agree with me, because you're smart and I'm right."
      - Charlie Munger

      Comment

      • 4Pics
        Confirmed User
        • Dec 2001
        • 7952

        #4
        Thanks, I'll play with this in a bit and post if I have any more questions.

        Comment

        Working...