【问题标题】:PHP - Match answer with two array and increase the countPHP - 将答案与两个数组匹配并增加计数
【发布时间】:2021-06-05 16:52:02
【问题描述】:

当用户提交 from 时,我得到了 40 个键值数据。如下所示。

$student_answer =  [
  1 => "rehabilitation of offenders",
  2 => "has been accelerating",
  3 => "all expectations",
  4 => "question the validity",
  5 => "than alleviate",
  6 => "more effective alternatives",
  7 => "social and economic",
  8 => "dcfgvhjk",
  9 => "vghbjnk",
  10 => "hbjnk",
  11 => "gvhbjn",
  12 => "vbhnm",
  13 => "fghj",
  14 => "fghj",
  15 => "vghbj",
  16 => "cgvhb",
  17 => "vbhn",
  18 => "vbn",
  19 => "fghj",
  20 => "gvhbj",
  21 => "cvbn",
  22 => "vbn",
  23 => "v bn",
  24 => "cfghjk",
  "25-26" =>  [
    0 => "D",
    1 => "E",
  ],
  27 => "fgvhbj",
  28 => "vbnm",
  29 => "v bnm",
  30 => "v bnm",
  31 => "n hb",
  32 => "hbjnkm",
  33 => "bnm",
  34 => "bnm",
  35 => "b nm",
  36 => "b nm",
  37 => "v bn",
  38 => "vbnm",
  "39-40" =>  [
    0 => "A",
    1 => "D",
  ],
];

我有正确的答案来自如下数据库:

$answer_from_database =  [
  1 => [
    0 => "rehabilitation of offenders"
  ],
  2 =>  [
    0 => "has been accelerating"
  ],
  3 =>  [
    0 => "all expectations"
  ],
  4 =>  [
    0 => "question the validity"
  ],
  5 =>  [
    0 => "than alleviate"
  ],
  6 =>  [
    0 => "more effective alternatives"
  ],
  7 =>  [
    0 => "social and economic"
  ],
  8 =>  [
    0 => "NOT GIVEN"
  ],
  9 =>  [
    0 => "FALSE"
  ],
  10 =>  [
    0 => "TRUE"
  ],
  11 =>  [
    0 => "TRUE"
  ],
  12 =>  [
    0 => "FALSE"
  ],
  13 =>  [
    0 => "FALSE"
  ],
  14 =>  [
    0 => "moral or philosophical"
  ],
  15 =>  [
    0 => "physiological/biological disposition"
  ],
  16 =>  [
    0 => "heritable/inherited"
  ],
  17 =>  [
    0 => "characterised by"
  ],
  18 =>  [
    0 => "hereditary aspect/biological basis"
  ],
  19 =>  [
    0 => "associated with"
  ],
  20 =>  [
    0 => "criminal tendencies"
  ],
  21 =>  [
    0 => "Chromosomal abnormality"
  ],
  22 =>  [
    0 => "masculine and aggressive"
  ],
  23 =>  [
    0 => "severely undermined by"
  ],
  24 =>  [
    0 => "environmental and social"
  ],
  "25-26 " =>  [
    0 => "D",
    1 => "E",
  ],
  27 =>  [
    0 => "means"
  ],
  28 =>  [
    0 => "for identification purposes"
  ],
  29 => [
    0 => "victims"
  ],
  30 =>  [
    0 => "centred on"
  ],
  31 =>  [
    0 => "removed"
  ],
  32 =>  [
    0 => "law enforcement agencies"
  ],
  33 =>  [
    0 => "evade capture"
  ],
  34 =>  [
    0 => "crime scene"
  ],
  35 =>  [
    0 => "the publication of"
  ],
  36 =>  [
    0 => "modern forensic techniques/forensic science"
  ],
  37 =>  [
    0 => "fingerprint-ing/new-found"
  ],
  38 => [
    0 => "standard practice"
  ],
  "39-40" => [
    0 => "A",
    1 => "D",
  ]
];

现在我想检查学生的答案是否与来自数据库的答案匹配,如果答案正确,我想增加$count的值,

到目前为止,我已经这样做了:

//$data = student's answer
//$answerKey = right answer

foreach($data as $key => $student){
            $count = 0;
            foreach($answerKey as $rkey => $right){
                
                if($student == implode (", ", $right)){
                    $count = $count + 1;
                }
                
                if(is_array($student)){
                    
                }
                
            }
        }

我卡在这里了,接下来我应该怎么做才能检查答案?

【问题讨论】:

    标签: php arrays associative-array


    【解决方案1】:

    这是可以做到的。

    $student_answer = array(
        1 => "Omnis molestias temp",
        2 => "Veritatis dolorem ab",
        3 => "Recusandae Ipsum au",
        4 => "Magnam fugiat deseru",
        5 => "Aut fugiat sunt eve",
        '6-7' => array(
            0 => 'asdasdasd',
            1 => 'adasdasdasda',
        )
    );
    
    $answer_from_database = array(
        1 => array (
            0 => "rehabilitation of offenders"
        ),
        2 => array (
            0 => "Veritatis dolorem ab"
        ),
        3 => array (
            0 => "all expectations"
        ),
        4 => array (
            0 => "question the validity"
        ),
        5 => array (
            0 => "than alleviate"
        ),
        '6-7' => array (
            0 => 'asdasdasd',
            1 => 'asdsdasdsa',
        ),
    );
    
    function get_score( $student, $answers ) {
        $count = 0;
        foreach ($student as $key => $answer) {
            $s_answer      = $answer;
            // check if answers exists
            if (array_key_exists($key, $answers)) {
                if (is_array($s_answer)) {
                    foreach ($s_answer as $sub_key => $sub_answer) {
                        if (array_key_exists($sub_key, $student[$key])) {
                            if ($sub_answer === $answers[$key][$sub_key]) {
                                $count = $count + 1;
                            }
                        }
                    }
                } else {
                    $actual_answer = $answers[$key][0];
                    if ($s_answer === $actual_answer) {
                        $count = $count + 1;
                    }
                }
            }
        }
        return $count;
    
    }
    
    $score = get_score( $student_answer, $answer_from_database );
    

    您应该查看的参考资料:-

    【讨论】:

    • 但答案也可以采用数组格式。那个怎么样?您已完成 1 个问题等于 1 个答案。情况永远不会如此。你看 25-26 可以在数组中有答案。
    • 您还没有解释在这种情况下计数将如何工作。在这种情况下,每次迭代计数都会增加一。这应该在问题中提及。除此之外,奇怪的是您的大部分数组键都是整数,然后是字符串。 @Gaurav
    • @Gaurav,我已经更新了我的答案,现在它可以用于两者。
    • 每个答案都有 1 分,所以如果 6-7 在数组中有 2 个索引,它就有 2 分。每个答案应该可以加1分
    • @Gaurav 已经实现了,你有没有再次检查我的答案。
    猜你喜欢
    • 2015-06-04
    • 2014-12-13
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多