Need PHP Function like avg()

Discussion in 'PHP' started by yelbom, Jun 18, 2011.

  1. #1
    I need a php function to take a list of numbers and to remove the numbers are not consistent.

    Example:

    100
    93
    109
    94
    23
    98
    102
    44

    So the php function would output the new list without the inconsistent numbers 23 and 44
     
    yelbom, Jun 18, 2011 IP
  2. waynewex

    waynewex Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What do you define as consistent? This shouldn't be a difficult feat to accomplish.

    
    <?php
    $numbers = array(100,93,109,94,23,98,102,44);
    
    $total_numbers = count($numbers);
    
    $sum = 0;
    foreach($numbers as $number){
        $sum = $sum + $number;
    }
    
    $average = $sum / $total_numbers;
    
    //what defines inconsistent?
    
    $inconsistent = 30;
    
    $range = $average - $inconsistent;
    
    $inconsistent_numbers = array();
    
    foreach($numbers as $number){
        if($number < $range){
            $inconsistent_numbers[] = $number;
        }
    }
    
    ?>
    
    PHP:
     
    waynewex, Jun 19, 2011 IP