Assemble question and answer array

Asked

Viewed 328 times

2

How do I mount this array, few times did foreach of unserialize, with that I’m having a hard time making this assembly.

Model

inserir a descrição da imagem aqui

PHP

<?php
// $campoBD é a simulação do valor vindo do mysql pela variável $row['prova'];

$campoBD = 'a:2:{i:0;a:7:{i:0;s:27:"<p>Questão 2 de teste:</p>";i:1;s:27:"<p>Questão 3 de teste:</p>";i:2;s:34:"<p>Primeira pergunta de teste:</p>";i:3;s:27:"<p>Questão 4 de teste:</p>";i:4;s:27:"<p>Questão 1 de teste:</p>";i:5;s:16:"<p>TESTE 51?</p>";i:6;s:16:"<p>teste 50?</p>";}i:1;a:7:{i:0;a:4:{i:0;s:18:"a) resposta 1 - Q2";i:1;s:18:"b) resposta 2 - Q2";i:2;s:18:"c) resposta 3 - Q2";i:3;s:18:"d) resposta 4 - Q2";}i:1;a:4:{i:0;s:18:"a) resposta 1 - Q3";i:1;s:18:"b) resposta 2 - Q3";i:2;s:18:"c) resposta 3 - Q3";i:3;s:18:"d) resposta 4 - Q3";}i:2;a:4:{i:0;s:20:"a) Resposta errada 1";i:1;s:21:"b) Resposta errada 2";i:2;s:18:"c) Resposta certa";i:3;s:21:"d) Resposta errada 3";}i:3;a:4:{i:0;s:18:"a) Muito obrigado!";i:1;s:11:"b) Socorro!";i:2;s:37:"c) “Grande nau, grande tormenta”.";i:3;s:39:"d) “A distância alimenta o sonho”.";}i:4;a:4:{i:0;s:18:"a) resposta 1 - Q1";i:1;s:18:"b) resposta 2 - Q1";i:2;s:18:"c) resposta 3 - Q1";i:3;s:18:"d) resposta 4 - Q1";}i:5;a:3:{i:0;s:4:"a) f";i:1;s:4:"b) r";i:2;s:4:"c) r";}i:6;a:3:{i:0;s:13:"a) sdjfjdfsjd";i:1;s:13:"a) sdjfjdfsjd";i:2;s:14:"sdjfsoijdfisoj";}}}'; 

$prova = unserialize($campoBD);

foreach($prova[0] as $rowPergunta) {
  echo $rowPergunta;

  $i = 0;
  foreach($prova[1] as $rowRespostas) {
    echo $rowRespostas[$i] . "<br>";

    $i++;
  }
}

2 answers

2


I have my doubts whether serializing the answers within the question record is the best solution. Even saving as JSON in this case would be much simpler since you only need to save structured data and not language objects. CSV would also solve the problem well.

But starting from the point after deserialization, where you own a array of questions and a array with the answers to each question, just ask:

foreach($prova[0] as $i => $rowPergunta) {
  echo $rowPergunta . "<br>";

  foreach($prova[1][$i] as $rowRespostas) {
    echo $rowRespostas[$i] . "<br>";
  }
}

But this is not a readable solution, because $prova[0] and $prova[1] are individual values in the code that does not express its function within it. It would be better to define a more semantic code:

list($perguntas, $respostas) = $prova;

foreach ($perguntas as $id => $pergunta) {
  echo $pergunta . '<br>';

  foreach ($respostas[$id] as $resposta) {
    echo $resposta . '<br>';
  }
}

Or reducing the second loop to a join:

list($perguntas, $respostas) = $prova;

foreach ($perguntas as $id => $pergunta) {
  echo $pergunta . '<br>';
  echo join('<br>', $respostas[$id]) . '<br>';
}

1

I don’t have your base, with this, I can’t simulate your query. But I will help you create the array() and how to access this array then with foreach(), and then you adapt to your need ok?

Following example:

<?
//Criando array de Perguntas
$perguntas[] = "Primeira Pergunta de Teste";
$perguntas[] = "Questão 1 de Teste";
$perguntas[] = "Questão 2 de Teste";
$perguntas[] = "Questão 3 de Teste";
$perguntas[] = "Questão 4 de Teste";

//Criando Array de Respostas
$respostas[] = array(
        "idPergunta" => "0",
        "a" => "resposta errada 1",
        "b" => "resposta errada 2",
        "c" => "Resposta Certa",
        "d" => "resposta errada 3"      
);

$respostas[] = array(
        "idPergunta" => "1",
        "a" => "resposta 1 - Q1",
        "b" => "resposta 2 - Q1",
        "c" => "Resposta 3 - Q1",
        "d" => "resposta 4 - Q1"        
);

$respostas[] = array(
        "idPergunta" => "2",
        "a" => "resposta 1 - Q2",
        "b" => "resposta 2 - Q2",
        "c" => "Resposta 3 - Q2",
        "d" => "resposta 4 - Q2"        
);

$respostas[] = array(
        "idPergunta" => "3",
        "a" => "resposta 1 - Q3",
        "b" => "resposta 2 - Q3",
        "c" => "Resposta 3 - Q3",
        "d" => "resposta 4 - Q3"    
);

//acessando via foreach
foreach($perguntas as $p => $kp){

    echo '<br><br>Perguntas: ' . $kp . '<br>';

    foreach($respostas as $r){
        if($r['idPergunta'] == $p){ //aqui comparo o índice da pergunta com a da Resposta
            echo '<pre>';
            var_dump($r);
        }
    }

}

I hope I’ve helped

  • Hello Leonardo, thank you so much for helping me. I updated my question, with the value of the table coming from mysql. It seems to be a little different from your model, as everything is within a single array. Take a look for kindness. Thank you.

  • I understand, but even with the return on the question it’s hard to simulate here. tries to interpret what I’ve given you. It is not difficult to work the structure. I unfortunately cannot simulate with the data you passed. I am sorry

  • Leandro, please copy this code http://ideone.com/kcW0qk and paste here http://phptester.net, will give to test. Just do not need to pay attention to preg_replace_callback, I needed to put in to be interpreted my serialize.

  • Good morning Leandro, took a look at the link above?

  • I have simplified my original code if I can take a look. Thank you.

Browser other questions tagged

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