How to join these two arrays

Asked

Viewed 47 times

-1

I have two array that are generated by inputs:

Question

<input type="hidden" name="pergunta[]" value="<?= $row_rs['pergunta']; ?>">

and Options

<input type="hidden" name="opcao[<?= $i; ?>][]" value="<?= strip_tags($rowOpcoes['opcao']); ?>">

How do I join the array [question] and [option] in a single variable, $prova, to save in the database?

INSERT

if ((isset($_POST["form"])) && ($_POST['form'] == "form1")) {

  $prova = array($_POST['pergunta'], $_POST['opcao']);

  $rs = $mysqli->prepare("INSERT INTO provas (codigo, titulo, prova, id_disciplina, data) VALUES (?, ?, ?, ?, ?) ");

  $rs->bind_param('sssss',
    $_POST['codigo'],
    $_POST['titulo'],
    $prova,
    $_POST['id_disciplina'],
    $_POST['data']
    );

  $rs->execute();
  $ultimoId = $rs->insert_id;


  if ($rs->errno) {
    echo 'Erro: ', $rs->error;
  } else {
    echo "<script>window.location='sucesso.php'</script>";
  }

}

print_r($_POST)

Array
(
    [id_disciplina] => 5
    [titulo] => Teste
    [pergunta] => Array
        (
            [0] => Questão 2 de teste:

            [1] => Questão 4 de teste:

            [2] => Questão 1 de teste:

            [3] => Questão 3 de teste:

            [4] => TESTE 50?


        )

    [opcao] => Array
        (
            [0] => Array
                (
                    [0] => a) resposta 1 - Q2
                    [1] => b) resposta 2 - Q2
                    [2] => c) resposta 3 - Q2
                    [3] => d) resposta 4 - Q2
                )

            [1] => Array
                (
                    [0] => a) Muito obrigado!
                    [1] => b) Socorro!
                    [2] => c) “Grande nau, grande tormenta”.
                    [3] => d) “A distância alimenta o sonho”.
                )

            [2] => Array
                (
                    [0] => a) resposta 1 - Q1
                    [1] => b) resposta 2 - Q1
                    [2] => c) resposta 3 - Q1
                    [3] => d) resposta 4 - Q1
                )

            [3] => Array
                (
                    [0] => a) resposta 1 - Q3
                    [1] => b) resposta 2 - Q3
                    [2] => c) resposta 3 - Q3
                    [3] => d) resposta 4 - Q3
                )

            [4] => Array
                (
                    [0] => a) f
                    [1] => b) r
                    [2] => c) r
                )

        )

    [button] => 
    [form] => form1
    [cod] => P5508182
    [data] => 2019-08-28
)

1 answer

2


To save the data in the question database and activities the way you intended, you can do the following:

$pergunta = $_POST['pergunta']:
$opcoes = $_POST['opcao']:



$prova = json_encode([$pergunta, $opcoes]);

To extract the data, you can reverse the result of your query:

$prova = json_decode($rs['prova']);

Obs: One of the problems of doing this way is that in the future you will always have to extract the data every time you edit...

But in json at least you have a line of data accessible via javascript.

Browser other questions tagged

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