It is only showing one value for each row
<p>I have a phpfiddle here: <a href="http://phpfiddle.org/main/code/get-rps" rel="nofollow">http://phpfiddle.org/main/code/get-rps</a></p>

<p>The problem I am getting is that in the <code>Total Marks</code> column, it is only display the total marks for the last question in all of the rows for all questions, rather than looping through all the total marks and displaying the correct total marks for each question. What am I doing wrong in the code?</p>

<p>Below is code:</p>

<pre><code>&lt;?php
$incorrect_ans = array(
                   array('A','C','D'),
                   array('B','C','D'),
                   array('A','B','D'),
                   array('A','B','C'));

$searchQuestionNo = array(
                   1,
                   2,
                   2,
                   3);

$totalMarks = array(
                   3,
                   5,
                   5,
                   2);

$ques_ans = array();    //to store incorrect answers against ques no.

$q_occ_count = array_count_values($searchQuestionNo);
foreach($searchQuestionNo as $key =&gt; $questionNo)
{
    if ( ! array_key_exists($questionNo, $ques_ans))
    {
        if($q_occ_count[$questionNo] === 1) //if a ques has only one correct ans
        {
            $ques_ans[$questionNo] = $incorrect_ans[$key];  //store the array of incorrect ans against the ques no as key 
        }
        else //if a ques has more than 1 correct ans
        {
            //find the intersection of incorrect_ans arrays for this ques
            $q_keys = array_keys($searchQuestionNo, $questionNo);
            $q_incorrect_ans = $incorrect_ans[$q_keys[0]];
            foreach($q_keys as $q_key) {
                $q_incorrect_ans = array_values(array_intersect($q_incorrect_ans, $incorrect_ans[$q_key]));
            }       
            $ques_ans[$questionNo] = $q_incorrect_ans;  //store the array of incorrect ans against the ques no as key
        }
    }
}
?&gt;
&lt;table border='1' id='penaltytbl'&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th class='questionnoth'&gt;Question No.&lt;/th&gt;
&lt;th class='answerth'&gt;Incorrect Answer&lt;/th&gt;
&lt;th class='marksth'&gt;Total Marks&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;?php

foreach($ques_ans as $questionNo =&gt; $inc_ans)
{
    $q_row_span = count($inc_ans);
    $row_count = 0;
    ?&gt;
    &lt;tr class="questiontd"&gt;

        &lt;!-- Question No --&gt;
        &lt;td class="questionnumtd q&lt;?php echo $questionNo?&gt;_qnum" rowspan="&lt;?php echo $q_row_span ?&gt;"&gt;
                &lt;?php echo $questionNo?&gt;&lt;input type="hidden" name="numQuestion" value="&lt;?php echo $questionNo?&gt;" /&gt;
        &lt;/td&gt;

        &lt;!-- first incorrect ans --&gt;
        &lt;td class="answertd"&gt;&lt;?php echo $inc_ans[$row_count]; ?&gt;&lt;/td&gt;

        &lt;!-- Marks --&gt;
       &lt;td class="totalmarkstd" rowspan="&lt;?php echo$q_row_span?&gt;"&gt;&lt;?php echo$totalMarks[$key]?&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;?php
        //remaining incorrect answers in separate row (if any) follows here
    if($row_count &lt; $q_row_span - 1) 
    {
        for($i=($row_count + 1); $i&lt;$q_row_span; $i++) { ?&gt;     
            &lt;tr&gt;&lt;td class="answertd"&gt;&lt;?php echo $inc_ans[$i]; ?&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;?php
        }
    }
}
?&gt;
&lt;/tbody&gt;
&lt;/table&gt;
</code></pre>
