dp198879 2013-04-28 19:12
浏览 47

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
)
  • 写回答

1条回答 默认 最新

  • dongluo3331 2013-04-28 21:16
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错