0

I am getting 40 key & value data when the user submits the from. Like below.

$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",
  ],
];

And I have right answer coming from database like below:

$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",
  ]
];

Now I want to check whether the student's answer match the answer coming from the database, and if the answer is correct, I want to increase the value of $count,

So far I have done this:

//$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)){
                    
                }
                
            }
        }

I got stuck here, what should I do next to check answer?

1 Answer 1

1

This is how it can be done.

$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 :-

7
  • But the answer can come in array format also. What about that? You have done 1 question is equal to 1 answer. That will always not be the case. You see 25-26 can have answer in array.
    – Gaurav
    Commented Jun 5, 2021 at 17:31
  • You haven't explained how would the counting work in that case. In that case the count will increase by one for each iteration. This should have been mentined inside the question. Apart from that its odd that you have most of your array key as integer and then some of then as string. @Gaurav
    – bhanu
    Commented Jun 5, 2021 at 17:55
  • @Gaurav, I have updated my answer and now it can be used for both.
    – bhanu
    Commented Jun 5, 2021 at 18:05
  • each answer holds 1 point so if 6-7 has 2 index in array, it holds 2 points. It should be able to add 1 point of each answer
    – Gaurav
    Commented Jun 6, 2021 at 2:54
  • @Gaurav its already implemented, have you checked my answer again.
    – bhanu
    Commented Jun 6, 2021 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.