如何根据重复的数组值创建新数组并计算平均值



我正在制作一个反馈系统,学生在其中为老师输入反馈,然后计算每个班级和各个科目的平均值。我该怎么做?

Array
(
[0] => Array
(
[empName] => Shahrukh Khan
[class] => Bachelor in Computer Applications 1st Year
[subjectName] => Basic Mathematics
[average] => 60
)
[1] => Array
(
[empName] => Shahrukh Khan
[class] => Bachelor in Commerce 1st Year
[subjectName] => Basic Mathematics
[average] => 60
)
[2] => Array
(
[empName] => Anshu Sharma
[class] => Bachelor in Computer Applications 1st Year
[subjectName] => Operating System
[average] => 75
)
)

我将此作为输出。

foreach ($result as $key => $value) {
$query = $this->Admin_model->getFeedbackReport($value->emp_uid,$value->subjectId,$month,$this->feedback1);
$excel = 0; $good = 0; $fair = 0;
foreach ($query as $key => $val) {
if($val->rating == "Excellent")
$excel = $excel+1;
elseif($val->rating == "Good")
$good = $good+1;     
else
$fair = $fair+1;
}
$finalAverage =0;
//echo $excel."<br>"; echo $good."<br>"; echo $fair;
$total = $excel+$good+$fair;
$excelWeight = $excel * 2;
$goodWeight = $good * 1;
$fairWeight = $fair * 0;
$marksGained = $excelWeight+$goodWeight+$fairWeight;
$maxScore = $total*2;
$average = round($marksGained/$maxScore * 100); 
$data[]= array(
'empName'       => $value->emp_name, 
'class'         => $value->course_name." ".$value->class_name,
'subjectName'   => $value->subjectName,
'average'       => $average
);
}        
//print_r($data);

我希望只要empName相同,就可以添加平均值(即:60(,并计算平均值(如60+60/2(。

预期输出-

+------+---------------+---------------------------------------+-----------------------+---------+-------+
| S.No |     Name      |                 Class                 |        Subject        | Average | Final |
+------+---------------+---------------------------------------+-----------------------+---------+-------+
|    1 | Shahrukh khan |  Bachelor in Commerce 1st Year        | Basic Mathematics     |      60 |    60 |
|    2 | Anshu Sharma  | Bachelor in Computer Applications 1st | Year Operating System |      75 |    75 |
+------+---------------+---------------------------------------+-----------------------+---------+-------+

事实上,SunjectClassAverage可以像我看到的那样重复。

该解决方案基于集合unique empNames,处理首次出现的index并求和平均值。

foreach语句之前加上$ar_emp = []; $ar_s = [];,然后替换:

$data[]= array(
'empName'       => $value->emp_name, 
'class'         => $value->course_name." ".$value->class_name,
'subjectName'   => $value->subjectName,
'average'       => $average
);    

代码如下:

$val_ename = $value->emp_name;
$val_class = $value->course_name." ".$value->class_name;
$val_sname = $value->subjectName;
$val_avera = $average;
if (!in_array($val_ename, $ar_emp)) { 
array_push($ar_emp, $val_ename); 
$ar_s[$val_ename] = array (
'coun'      => 1,
'average'   => $val_avera,
'inde'      => $key 
);
$data[]= array(
'empName'       => $val_ename, 
'class'         => $val_class,
'subjectName'   => $val_sname,
'last_average'  => $val_avera,
'final'         => $val_avera
); 
} else {
$ar_s[$val_ename]['coun'] += 1;
$ar_s[$val_ename]['average'] += $val_avera;
$data[$ar_s[$val_ename]['inde']]= array(
'empName'       => $val_ename, 
'class'         => $val_class,
'subjectName'   => $val_sname,
'last_average'  => $val_avera,
'final'         => ($ar_s[$val_ename]['average'] / $ar_s[$val_ename]['coun'])
);               
}   

相关内容

  • 没有找到相关文章

最新更新