PHP:按类别平均评分

I'm fairly new to PHP and trying to sort a class project. I've banged away and can't see to sort it out, this is the last remaining piece.

I have a form - submit essentially 2 arrays I'm trying to determine the average score for based upon the category groupings. 1. ($_post['category']), ($_post['score']) - with category being a text based array and score obviously being numbers based.

How would I go about finding the average score for each category? I've tried various options to for mapping the two arrays and then trying to average the grades. I can get the total score average pretty easily, but finding a ways to do this by each category individually is tripping me up.

I need to apply a weight to each category score, but that seems pretty easy once you get the above sorted.

Thanks, Steve

$_POST example:

Array (
    [0] => Assignment
    [1] => Assignment
    [2] => Assignment
    [3] => Assignment
    [4] => Assignment
    [5] => Assignment
    [6] => Exam
    [7] => Assignment
    [8] => Assignment
    [9] => Assignment
    [10] => Exam
    [11] => Exam
    [12] => Final Project
)
Array (
    [0] => 100
    [1] => 100
    [2] => 100
    [3] => 98
    [4] => 90
    [5] => 92
    [6] => 100
    [7] => 100
    [8] => 100
    [9] => 100
    [10] => 100
    [11] => 92
    [12] => 100
)

There are several ways to go about this, here's one:

// create arrays
$cat = array(
    "Assignment", "Assignment", "Assignment", 
    "Assignment", "Assignment", "Assignment", 
    "Exam", "Assignment", "Assignment", 
    "Assignment", "Exam", "Exam", "Final Project");

$grade = array(100,100,100,98,90,92,100,100,100,100,100,92,100);

// create $result: key = cat, values = sum, count, avg = 0
$result = array_fill_keys($cat, array('sum' => 0, 'count' => 0, 'avg' => 0));

$max = count($cat);

// add grades to category in $result[cat][sum]
for ($i = 0; $i < $max; $i++) 
   $result[$cat[$i]]['sum'] += $grade[$i];

// count number of categories in $cat
$num = array_count_values($cat);

// fill $result[cat][count]...
// then divide each cat's sum by each cat's count => $result[cat][avg]
foreach ($result as $k => $v) {
    $result[$k]['count'] = $num[$k];
    $result[$k]['avg'] = round($result[$k]['sum'] / $num[$k], 2);
}

Result:

Array
(
    [Assignment] => Array
        (
            [sum] => 880
            [count] => 9
            [avg] => 97.78
        )

    [Exam] => Array
        (
            [sum] => 292
            [count] => 3
            [avg] => 97.33
        )

    [Final Project] => Array
        (
            [sum] => 100
            [count] => 1
            [avg] => 100
        )

)

see it working: http://codepad.viper-7.com/QQAaXD