Answer the question
In order to leave comments, you need to log in
How to detect a strong deviation in an array?
There is an array with prices:
$prices = array(
'10300',
'10200',
'1250',
'1260',
'1240',
'1140',
'20',
'30'
);
Answer the question
In order to leave comments, you need to log in
Ideally, you need to somehow calculate the cost. By weight, by product material, by its brand, country of assembly, etc. Then add conditional 20%. This will be the "red" price (not to be confused with the average).
Next, you need to decide what deviation is acceptable.
from the comments: if you remove all the numbers whose deviation is more than 100% from the average, then 20 and 30 will remain.
so just choose the Threshold you need, you can tie it to $avg.
$prices = array(
'10300',
'10200',
'1250',
'1260',
'1240',
'1140',
'20',
'30'
);
$avg = array_sum($prices)/count($prices);
$threshold = 2100;
$result = array_filter($prices, function ($p) use ($avg, $threshold) {
return $p < ($avg + $threshold) && $p > ($avg - $threshold) ? $p : false;
});
var_dump($result);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question