Php two-layer foreach loop how to set the initial value of the in-layer loop variable = 0

Source: Internet
Author: User
Php two-layer foreach loop how to set the initial value of the in-layer loop variable to 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


Reply to discussion (solution)

I don't understand what you mean, for example. Or how you want to output it.

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.

 $question): ?>        
 
 
.

    $answer): ?>


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.



 '; $ Questions = array ('Issue 1', 'issue 2', 'issue 3'); $ answers = array ('option ', '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

#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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.