Add item in array with PHP foreach

Asked

Viewed 56 times

0

I need to pick with the foreach of PHP, the id of the question and the reply of each answered question.

I can only get the id of the question, as I would also get the typed answer?

Man foreach

<?
 if (isset($_POST['resposta'])) {


 foreach ($_POST['id_pergunta'] as $value) {

     echo $value."<br><br>";


 }



 }
?>

I have the form like this:

<form role="form" action="" method="post">





                          <div class="row setup-content" id="step-1" style="display: block;">
        <div class="col-xs-12">
            <div class="col-md-12">
                <h3 style="color: #0093cf;text-transform: uppercase;">teste1</h3>
                <div class="form-group">

                </div>
                <div class="form-group">
                    <label class="control-label">Resposta</label>
                    <input maxlength="100" type="text" id="resposta[]" name="resposta[]" required="required" class="form-control" placeholder="Resposta da pergunta">
                    <input type="hidden" id="id_pergunta[]" name="id_pergunta[]" required="required" class="form-control" value="1">
                </div>
                <button class="btn btn-primary nextBtn btn-lg pull-right" type="button">Próxima Pergunta</button>
            </div>
        </div>
    </div>






            <div class="row setup-content" id="step-2" style="display: none;">
            <div class="col-md-12">
                <h3 style="color: #0093cf;text-transform: uppercase;">teste2</h3>
                <div class="form-group">

                </div>
                <div class="form-group">
                    <label class="control-label">Resposta</label>
                    <input maxlength="100" id="resposta" name="resposta[]" type="text" required="required" class="form-control" placeholder="Resposta da pergunta">
                    <input type="hidden" id="id_pergunta" name="id_pergunta[]" required="required" class="form-control" value="2">
                </div>
                <button class="btn btn-primary nextBtn btn-lg pull-right" type="button">Próxima Pergunta</button>
            </div>
    </div>






            <div class="row setup-content" id="step-3" style="display: none;">
            <div class="col-md-12">
                <h3 style="color: #0093cf;text-transform: uppercase;">teste3</h3>
                <div class="form-group">

                </div>
                <div class="form-group">
                    <label class="control-label">Resposta</label>
                    <input maxlength="100" id="resposta" name="resposta[]" type="text" required="required" class="form-control" placeholder="Resposta da pergunta">
                    <input type="hidden" id="id_pergunta" name="id_pergunta[]" required="required" class="form-control" value="3">
                </div>
                <button class="btn btn-primary nextBtn btn-lg pull-right" type="button">Próxima Pergunta</button>
            </div>
    </div>






            <div class="row setup-content" id="step-4" style="display: none;">
            <div class="col-md-12">
                <h3 style="color: #0093cf;text-transform: uppercase;">teste4</h3>
                <div class="form-group">

                </div>
                <div class="form-group">
                    <label class="control-label">Resposta</label>
                    <input maxlength="100" id="resposta" name="resposta[]" type="text" required="required" class="form-control" placeholder="Resposta da pergunta">
                    <input type="hidden" id="id_pergunta" name="id_pergunta[]" required="required" class="form-control" value="4">
                </div>
                <button class="btn btn-primary nextBtn btn-lg pull-right" type="button">Próxima Pergunta</button>
            </div>
    </div>






            <div class="row setup-content" id="step-5" style="display: none;">
            <div class="col-md-12">
                <h3 style="color: #0093cf;text-transform: uppercase;">teste5</h3>
                <div class="form-group">

                </div>
                <div class="form-group">
                    <label class="control-label">Resposta</label>
                    <input maxlength="100" id="resposta" name="resposta[]" type="text" required="required" class="form-control" placeholder="Resposta da pergunta">
                    <input type="hidden" id="id_pergunta" name="id_pergunta[]" required="required" class="form-control" value="5">
                </div>
                <button class="btn btn-primary nextBtn btn-lg pull-right" type="button">Próxima Pergunta</button>
            </div>
    </div>










    <div class="row setup-content" id="step-6" style="display: none;">
        <div class="col-xs-12">
            <div class="col-md-12">
                <h3> Obirgado por participar</h3>

                <button type="submit" name="mudar_atendente" class="btn btn-success btn-lg pull-right">Enviar Respostas</button>
            </div>
        </div>
    </div>
</form>
  • When you say you want to get the question id, do you refer to the input id attribute? Sorry, that’s not clear to me yet.

  • that’s right, take the value of the id_question and also the answer

1 answer

3


How it comes to 2 arrays can be done as follows:

foreach(array_combine($_POST['id_pergunta'], $_POST['resposta']) as $p => $r)
{

   echo $p." ".$r.PHP_EOL;

}

array_combine - Create an array using an array for keys and another for values

ideone example

Browser other questions tagged

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