PHP获得平均成绩

时间:2014-12-23 07:26:42

标签: php arrays foreach

我需要获得函数中平均成绩的数量。似乎没有完整的工作..感谢您的帮助。 这就是我到目前为止所做的:

<?php
function povprecje($d,$t){
    $v=0;

    foreach($t as $x=>$y){
        foreach($y as $d=>$grade){
            $v = $v+$grade;
            $v = $v/count($grade);

            return $v;
        }
    }
}

$t = array(
    "Student" => array(
        "math"    => 3,
        "algebra" => 4,
        "science" => 4
    )
);

echo povprecje("math",$t);
?>

1 个答案:

答案 0 :(得分:0)

也许你想实现这个目标?

function povprecje($sSubject, $aGrades) {
    $iGradesSum     = 0;
    $iAmount        = 0;    

    foreach($aGrades as $aStudentGrades) {
        if(isset($aStudentGrades[$sSubject])) {
            $iGradesSum+= $aStudentGrades[$sSubject];
            $iAmount++;
        }
    }

    return $iGradesSum / $iAmount;
}

函数调用:

echo povprecje("math", $t);