PHP - 将答案与两个数组匹配并增加计数

PHP - Match answer with two array and increase the count

当用户提交来自时,我得到了 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)){
                    
                }
                
            }
        }

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

这是可以做到的。

$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 );

References that you should look at :-