Add item in array with PHP foreach

0

I need to get the PHP foreach , the question id , and the answer of each question answered.

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

My 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>
    
asked by anonymous 11.10.2018 / 20:26

1 answer

3

How to deal with 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 array for values

ideone example

    
11.10.2018 / 20:49