How to compare responses and templates where each is a PHP string

Asked

Viewed 123 times

2

I have 2 PHP strings that are the student’s responses and feedback. I need to compare them and show the result in a table:

$respostas='1;A|2;B|3;C';
$gabaritos='1;A|2;B|3;D';

I need him to generate a table at the end as follows:

Pergunta | Resposta | Gabarito
   1     |    A     |    A
   2     |    B     |    B
   3     |    C     |    D

I made a explode to separate the questions, then a foreach to bring each result and then another explodes to separate the questions and responses to assemble the table of responses, but I do not know how to include the feedback within this table:

$gbarray = explode('|',$respostas);
foreach($gbarray as $resposta){
        $questoes = explode(';',$resposta);
        $questao = $questoes [0];
        $resposta = $questoes [1];
echo '
      <tr>
            <td>Matéria</td>
            <td>'.$questao.'</td>
            <td>'.$resposta.'</td>
            <td>Reposta Gabarito</td>
            <td>Pontuação</td>
            <td>% Acerto dos Membros</td>
      </tr>
}
  • Search what the function array_map does when you set the first parameter to NULL

1 answer

2


Use the syntax:

foreach($rparray as $key => $resposta){
            ↑        ↑           ↑
          array    índice      valor

Then just make an explosion in the items of $gbarray using as an index the value of $key. Below I renamed the variables for organization effect:

<?
$respostas='1;A|2;B|3;C';
$gabaritos='1;A|2;B|3;D';

$rparray = explode('|',$respostas);
$gbarray = explode('|',$gabaritos);

echo '<table>';

foreach($rparray as $key => $resposta){
        $questoes = explode(';',$resposta);
        $gabaritos = explode(';',$gbarray[$key]);
        $questao = $questoes [0];
        $resposta = $questoes [1];
        $gabarito = $gabaritos[1];
echo '
      <tr>
            <td>Matéria</td>
            <td>'.$questao.'</td>
            <td>'.$resposta.'</td>
            <td>'.$gabarito.'</td>
            <td>Pontuação</td>
            <td>% Acerto dos Membros</td>
      </tr>';
}
echo '</table>';
?>

Upshot:

inserir a descrição da imagem aqui

IDEONE

Browser other questions tagged

You are not signed in. Login or sign up in order to post.