Php two-layer foreach loop how to set the initial value of the in-layer loop variable to 0 & lt ;? Php & nbsp; foreach ($ questions & nbsp; as & nbsp; $ k & gt; $ question): & nbsp ;? & Gt; & nbsp; & nbsp php two-layer foreach loop how to set the initial value of the in-layer loop variable = 0
$ Question):?>
$ Answer):?>
How to enable the $ I value to start from 0 after the second layer loop after the layer loop, instead of continuing the value of the previous loop
------ Solution --------------------
I don't understand what you mean, for example. Or how you want to output it.
------ Solution --------------------
Reference:
Quote: reference:
I don't understand what you mean, for example. Or how you want to output it.
The requirement is this. the following several options are related to one-to-many options. I wrote two foreach loop data that can be read,
For example, in question 1, the following option is 1. option a 2. option B Question 2 the following option is 1. option c 2. option d I want to use $ I for the option number here, but the option number of question 2 after two foreach starts from 3.
Echo'
';
$ Questions = array (
'Question 1 ',
'Question 2 ',
'Question 3'
);
$ Answers = array (
Array ('option A', 'Option B '),
Array ('option C', 'option D '),
Array ('option E', 'option f ')
);
Foreach ($ questions as $ k => $ question ){
Echo 'q'. ($ k + 1 ).'
';
Foreach ($ answers [$ k] as $ I => $ answer ){
Echo 'option'. ($ I + 1). ''. $ answer .'
';
}
Echo'
';
}
?>
Q1
Option1 option
Option2 option B
Q2
Option1 option c
Option2 option d
Q3
Option1 option e
Option2 option f
------ Solution --------------------
#3 code
Change row 8th
Change row 14th
Each time the inner layer traverses the $ answers array, and takes only a small amount of data.
We recommend that you adjust the data organization method (reference #4)
Of course, you can also perform clustering on $ answers before the main loop starts.