title: Grading using weighted averages in php
body: <p>How could I grade this test with php? I need a percentage score...</p>
  
  <p>I have an array of questions containing the correct / incorrect bool and the corresponding weight.</p>
  
  <p>Do I need to find the average of correct answers first?</p>
  
  <p>What would the equation be?</p>
  
  <pre><code>$questions = array(
      0=&gt;array(
          'Question'=&gt;"Some Question",
          'Correct'=&gt;true,
          'Weight'=&gt;5,
      ),
      1=&gt;array(
          'Question'=&gt;"Some Question",
          'Correct'=&gt;false,
          'Weight'=&gt;5,
      ),
      2=&gt;array(
          'Question'=&gt;"Some Question",
          'Correct'=&gt;true,
          'Weight'=&gt;4,
      ),
      3=&gt;array(
          'Question'=&gt;"Some Question",
          'Correct'=&gt;true,
          'Weight'=&gt;0,
      ),
      4=&gt;array(
          'Question'=&gt;"Some Question",
          'Correct'=&gt;false,
          'Weight'=&gt;5,
      ),
      5=&gt;array(
          'Question'=&gt;"Some Question",
          'Correct'=&gt;true,
          'Weight'=&gt;4,
      ),
  );
  $weights = array(
      0=&gt;0
      1=&gt;0
      2=&gt;.05
      3=&gt;.20
      4=&gt;.25
      5=&gt;.50
  );
  $totalQuestions=0;
  $correctAnswers=0;
  $points=0;
  foreach($questions as $question){
      $totalQuestions++;
      if($question['Correct']){
          $correctAnswers++;
          $points = $points = $weights[$question['Weight'];
      }
  }
  </code></pre>