[英]How to get average data for current month with conditions?
I want to display average of high, low and normal rates in my chart, but not sure how to get the average.我想在我的图表中显示高、低和正常率的平均值,但不知道如何获得平均值。 Here is what I have tried..
这是我尝试过的..
$query = Rates::select('value')
->whereMonth('date',$val)
->where('user_id',$user_id)
->get();
$total = count($query);
$low = 0;
$high = 0;
$normal = 0;
if($total > 0) {
foreach ($query as $val) {
if($val->value < 60){
$low++;
} else if ($val->value > 90){
$high++;
} else {
$normal++;
}
}
}
You can keep an array of values and then run an average calculation on them after populating您可以保留一组值,然后在填充后对它们进行平均计算
$low = [];
$high = [];
$normal = [];
if($total > 0) {
foreach ($query as $val) {
if($val->value < 60) $low[]=$val->value;
else if ($val->value > 90) $high[]=$val->value;
else $normal[]=$val->value;
}
}
$lowavg = count($low) > 0 ? array_sum($low)/count($low) : 0;
$highavg = count($high) > 0 ? array_sum($high)/count($high) : 0;
$normalavg = count($normal) > 0 ? array_sum($normal)/count($normal) : 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.