http://phpfiddle.org/main/code/get-rps
Now it is not shown in the above fiddle that it is able to insert all the data. But in application it is not displaying all incorrect answers in the table.
The error for this is caused by the fact that array is iterating over $ques_ans
with for() has a gap in keys.
var_dump($ques_ans)
gives us:
array(3) {
... skipped for brevity
[2]=>
array(2) {
[0]=>
string(1) "B"
[2]=>
string(1) "D"
}
... skipped for brevity
}
There is no element with the key [1]
. It's because the function array_intersect
, which I use on line 49, preserves keys.
To quickly fix the code just to make it work without errors I added array_values()
on line 51:
$ques_ans[$questionNo] = array_values($q_incorrect_ans); //store the array of incorrect ans against the ques no as key
But it still does have missing keys and hence not all incorrect answers are displayed. What else am I doing wrong here?
The big problem is that if a question has multiple correct answers, then in the array for incorrect answers, it displays multiple sets of answers for those questions and just removes one correct answer at a time for each set of answers. You will see this in the array for incorrect answers.
Below is a screenshot showing the a missing incorrect answer and a missing key but how can I fix this?