Array from nested (dependent) fields in $_POST?

There is a form with this structure:
<label>Question No. 1</label>
<input type="text" name="???">
<label>1 answer to 1 question</label>
<input type="text" name="???">
<label>2 the answer to question 1</label>
<input type="text" name="???">
<label>3 response to question 1</label>
<input type="text" name="???">

<label>Question No. 2</label>
<input type="text" name="???">
<label>1 answer to 1 question</label>
<input type="text" name="???">
<label>2 the answer to question 1</label>
<input type="text" name="???">


Number of different for each question.

I need to do:
a value of 1 for the question record in the database
- a value of 1 answer to 1 question write to database
- a value of 1 answer to 1 question write to database
...
value 5 question record in the database
- a value of 1 for response to question 5 write to database
value 2 response to question 5 write to database

Record will be as follows:
- received a value of 1 for the question, recorded in the table and got id
- received a value of 1 answer to 1 question, recorded in another table, the answer id of the question
- received a value of 2 answer to question 1, recorded in another table, the answer id of the question
...
- received a value of 5 to the question, recorded in the table and got id
- received a value of 1 for the response to question 5, recorded in another table, the answer id of the question
- received a value of 2 answer to question 5, recorded in another table, the answer id of the question

Saw this design:
<label>Question No. 1</label>
<input type="text" name="question[][]">
<label>1 answer to 1 question</label>
<input type="text" name="answer[][]">
<label>2 the answer to question 1</label>
<input type="text" name="answer[][]">
<label>3 response to question 1</label>
<input type="text" name="answer[][]">

<label>Question No. 2</label>
<input type="text" name="question[][]">
<label>1 answer to 1 question</label>
<input type="text" name="answer[][]">
<label>2 the answer to question 1</label>
<input type="text" name="answer[][]">


But I do not understand how it works and how you can use.
April 19th 20 at 12:11
2 answers
April 19th 20 at 12:13
Solution
<label>Question No. 1</label>
<input type="text" name="question[]">
<label>1 answer to 1 question</label>
<input type="text" name="answer[]">
<label>2 the answer to question 1</label>
<input type="text" name="answer[]">
<label>3 response to question 1</label>
<input type="text" name="answer[]">

Then just iterate through arrays with the answers, to name for example answer1[], answer2[]
Thanks for the reply! Sorry, not quite understand about
just go through the arrays with a name for example answer1[], answer2[]
- garrison.Smi commented on April 19th 20 at 12:16
@garrison.Smi, well, variant two-dimensional array you to anything will not lead, I think it would be better to do so:
<label>Question No. 1</label>
 <input type="text" name="questions[]">
 <label>1 answer to 1 question</label>
 <input type="text" name="answers[1][]">
 <label>2 the answer to question 1</label>
 <input type="text" name="answers[1][]">
 <label>3 response to question 1</label>
 <input type="text" name="answers[1][]">

 <label>Question No. 2</label>
 <input type="text" name="questions[]">
 <label>1 answer to 1 question</label>
 <input type="text" name="answers[2][]">
 <label>2 the answer to question 1</label>
 <input type="text" name="answers[2][]">

$c = sizeof($question);
for($i =0;$i<$c;++$i;){
//the sorting issues
 $cc = sizeof($answers[$i]);
 for($i =0;$i<$cc;++$i;){
 //iterate through all the answers
}
}
- deo commented on April 19th 20 at 12:19
@deo something, or magnetic storm, or I'm stupid as a boot, but I did not get the correct conclusion))
$c = sizeof($question);
 for($i = 0; $i<$c; ++$i){
 foreach ($question as $question_name) {
 echo $question_name.'<br>';
}
 $cc = sizeof($answers[$i]);
 for($i = 0; $i < $cc; ++$i){
 foreach ($answers as $answer) {
 foreach ($answer as $answer_name) {
 echo $answer_name.'<br>';
}
}
}
 }
- garrison.Smi commented on April 19th 20 at 12:22
@garrison.Smi,
$c = sizeof($question);
for ($i = 0; $i < $c; ++$i) {
 echo $question[$i];
 $cc = sizeof($answers[$i]);
 for ($ii = 0; $ii < $cc; ++$ii) {
 echo $answers[$i][$ii];
}
}
- deo commented on April 19th 20 at 12:25
@deo, Do so
<form action="" method="POST">
 <input type="text" name="question[]" value="Question 1">
 <input type="text" name="answer[1][]" value="Answer 1 for question 1">
 <input type="text" name="answer[1][]" value="Answer 2 for question 1">
 <input type="text" name="answer[1][]" value="Answer 3 for question 1">

 <input type="text" name="question[]" value="Question 2">
 <input type="text" name="answer[2][]" value="Answer 1 for question 2">
 <input type="text" name="answer[2][]" value="Answer 2 for question 2">

 <input type="text" name="question[]" value="Question 3">
 <input type="text" name="answer[3][]" value="Answer 1 for question 3">
 <input type="text" name="answer[3][]" value="Answer 2 3 question">
 <input type="text" name="answer[3][]" value="Answer 3 for question 3">
 <input type="text" name="answer[3][]" value="Answer 4 for question 3">

 <input type="submit" name="test" value="Test">
</form>


$c = sizeof($question);
for($i =0;$i<$c;++$i){
 echo $question[$i].'<br>';

 $cc = sizeof($answers[$i]);
 for($ii = 0; $ii < $cc; ++$ii){
 echo $answers[$i][$ii].'<br>';;
}
}


Goes like this:
5e9757e775b59406538453.png - garrison.Smi commented on April 19th 20 at 12:28
@garrison.Smi, try to either start with [0], or write
$c = sizeof($question);
for($i =0;$i<$c;++$i){
 echo $question[$i].'<br>';

 $cc = sizeof($answers[$i]);
 for($ii = 1; $ii < $cc; ++$ii){
 echo $answers[$i][$ii].'<br>';;
}
}

replacing 0 to 1 - deo commented on April 19th 20 at 12:31
@deo, thanks for the help and patience! - garrison.Smi commented on April 19th 20 at 12:34
April 19th 20 at 12:15
<label>Question No. 1</label>
<input type="text" name="question[1]">
<label>1 answer to 1 question</label>
<input type="text" name="answer[1][1]">
<label>2 the answer to question 1</label>
<input type="text" name="answer[1][2]">
<label>3 response to question 1</label>
<input type="text" name="answer[1][3]">

<label>Question No. 2</label>
<input type="text" name="question[2]">
<label>1 answer to 1 question</label>
<input type="text" name="answer[2][1]">
<label>2 the answer to question 1</label>
<input type="text" name="answer[2][2]">


In PHP, respectively, but all will be at once decomposed and available as an array
$_POST['answer'][1][1]
$_POST['answer'][1][2]
etc

Find more questions by tags PHP