How the PHP two-layer foreach loop sets the initial value for the inner loop variable =0

Source: Internet
Author: User
$question):?>
$answer):?>




How to allow the value of $i to start at 0 instead of the value of the last loop after the second inner loop after the inner loop is completed


Reply to discussion (solution)

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

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


The requirement is such a problem the following several options questions are a one-to-many relationship with options I wrote two foreach loop data to read,
For example, question 1 The following option is 1. Option a 2. Option B question 2 The following option is 1. Option C 2. Option d The title of the option here I want to do with $i, but two foreach after question 2 is the number of the option that starts with 3

 $question):?>
 
 
.
    $answer):?>


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


The requirement is such a problem the following several options questions are a one-to-many relationship with options I wrote two foreach loop data to read,
For example, question 1 The following option is 1. Option a 2. Option B question 2 The following option is 1. Option C 2. Option d The title of the option here I want to do with $i, but two foreach after question 2 is the number of the option that starts with 3



 
  '; $questions = array (    ' problem 1 ', ' question    2 ',    ' question 3 '); $answers = 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 A
Option2 Option B

Q2
Option1 option C
Option2 option D

Q3
Option1 option E
Option2 option F

#3 Code
The 8th line should read
The 14th line should read

The inner layer iterates through the $answers array each time, and takes only a small amount of data
It's not worth the candle, it is recommended to adjust the data organization method (see # #)

Of course, you can cluster the $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.