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
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++;
}
}
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.
– Tiago
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
– user148170
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.– Tiago
Good morning Leandro, took a look at the link above?
– Tiago
I have simplified my original code if I can take a look. Thank you.
– Tiago