Table list to be dynamic require with skip approach using multi arrays

I want to be build logic as with below desired result. I have multi arrays which to be listed in table as questionnaire along with skip approach.

Helps are definitely appreciated

Fiddle link also mentioned it :- PhpFiddle - PHP/MySQL in-browser IDE and online server

<?php
$users = array (
    0  => array("user_id" => "217", "user_name" => "S", "id" => "33"),
    1 => array("user_id" => "216", "user_name" => "A", "id" => "32"),
    2  => array("user_id" => "215", "user_name" => "B", "id" => "31"),
);
$questions = array (
    0  => array("text" => "Q1", "type" => "text", "qid" => "1"),
    1  => array("text" => "Q2", "type" => "text", "qid" => "2"),
    2  => array("text" => "Q3", "type" => "text", "qid" => "3"),
);
$answers = array (
    0  => array("SRI" => "31", "qid" => "1", "answer" => "A1"),
    1  => array("SRI" => "31", "qid" => "2", "answer" => "A2"),
    2  => array("SRI" => "31", "qid" => "3", "answer" => "A3"),
    3  => array("SRI" => "32", "qid" => "3", "answer" => "A3"),
    4  => array("SRI" => "32", "qid" => "2", "answer" => "A2"),
    5  => array("SRI" => "33", "qid" => "1", "answer" => "A1"),
    6  => array("SRI" => "33", "qid" => "3", "answer" => "A3")
);
//echo "<pre>";
//print_r($users);
//print_r($questions);
//print_r($answers);
?>
<table border = 1>
    <tr>
        <th>
            User
        </th>
        <?php 
foreach($questions as $key => $Qval){
    echo "<th>".$Qval['text']."</th>";         
}
        ?>
    </tr>
    <?php
foreach($users as $key => $Uval){
    echo "<tr>";  
    echo "<td>".$Uval['user_name']."</d>";
    foreach($questions as $key => $Qval){
        foreach($answers as $key => $Aval){
            if (($Qval['qid'] == $Aval['qid']) && ($Uval['id'] == $Aval['SRI'])){
                echo "<th>".$Aval['answer']."</th>";
            }
        }
    }
    echo "</tr>";  
}
    ?>
</table>

Desired Result

41%20AM

Reference :- Stackoverflow

You need to divise your problem in two steps :

  1. detect if the user answer the current question or not

  2. then display the result

     foreach($users as $key => $Uval){
         echo "<tr>";  
         echo "<td>".$Uval['user_name']."</d>";
         foreach($questions as $key => $Qval){
             $userAnswer = null ; // no user answer per default
             foreach($answers as $key => $Aval){ // loop to find a user answer 
                 if (($Qval['qid'] == $Aval['qid']) && ($Uval['id'] == $Aval['SRI'])){
                     $userAnswer = $Aval['answer']; // save the user answer
                     break ; // we found the user answer, no need to continue to loop over the remaining answers
                 }
             }
             // display the user answer, use a placeholder if none is found
             echo '<td>' . (is_null($userAnswer) ? 'SKIP' : $userAnswer) . '</td>' ;
         }
         echo "</tr>";  
     }
Sponsor our Newsletter | Privacy Policy | Terms of Service