0

I have an array like this: 102, 97, 101, 1, 107, 95, 555. I need to exclude numbers, which very differ from other. So array should be: 102, 97, 101, 107, 95. How can I do this in php?

4
  • 4
    1) Have you tried something 2) What means/define very differ from each other ? Commented Aug 3, 2015 at 13:05
  • What will be criteria to identify too high or low? Commented Aug 3, 2015 at 13:10
  • what code have you tried so far? Commented Aug 3, 2015 at 13:12
  • This seems to be unclear did you defined the list of array that need to be excluded Commented Aug 3, 2015 at 13:16

2 Answers 2

1
function getAverageArray($min, array $arr){
    $arr2 = array($arr[0]);
    foreach(array_slice($arr,1) as $val)
        if ($val - $arr[0] < $min && $arr[0] - $val < $min)
            $arr2[] = $val;
    return $arr2;
}

//the minimum difference necessary
$min = 90;
$arr = array(102, 97, 101, 1, 107, 95, 555);

//Array ( [0] => 102 [2] => 97 [3] => 101 [4] => 107 [5] => 95 )
print_r(getAverageArray($min,$arr));
Sign up to request clarification or add additional context in comments.

2 Comments

These code work incorrect if the first number much higher then other. For example: (999, 102, 97, 101, 1, 107, 95) -> (999)
Well yes basically or you use the first key or you pass the average number, you could even compute them all...
0

That's possible but you have to set the threshold.

  1. get the average of all the values

  2. exclude values whose `abs(value-avergage) > your_threshold``

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.