Problems related to the use of foreach to loop two-dimensional arrays

Source: Internet
Author: User
If you use foreach to loop the two-dimensional array, the second loop will not be displayed, but if you comment out the first loop, the content of the second loop will be displayed normally, which of your friends knows the reason? thank you very much! PHPcode $ products2array (array (name & gt; Apple, quantity & gt; 6, unit price & gt; 12.2)
The second loop will not be displayed in the second loop. However, if you comment out the first loop, the content of the second loop will be displayed normally,
Which of your friends knows the reason? thank you very much!


PHP code
  $ Products2 = array ('name' => 'apple', 'qty '=> 6, 'unit price' => 12.2 ), array ('name' => 'banana ', 'quantity' => 2, 'unit price' => 15.1), array ('name' => 'watermelon ', 'qty '=> 3, 'unit price' => 25.3); // Loop 1 for ($ i1 = 0; $ i1
  ';} // Loop 2 // reset ($ products2); even if a reset pointer is added, the following loop cannot be displayed, but if you comment out the loop, loop 2 is displayed normally. if you know the reason, please let me know. thank you! For ($ i2 = 0; $ i2
  ';}


Online, etc. thank you!

------ Solution --------------------
This may also require sub-array pointers to be reset.

For has no pointer problem,

Foreach, the pointer may be moved. For more information, see test.

Each must be a mobile pointer.


You can use for, foreach, and each to test them separately.

------ Solution --------------------
Your first round of traversal is for $ products2 [$ i1] instead of $ products2. In other words, the reset () is $ products2 [$ i1], and the $ products2 pointer has never changed.
------ Solution --------------------
PHP code
  'Apple', 'qty '=> 6, 'unit price' => 12.2), array ('name' => 'ban', 'qty' => 2, 'Unit price' => 15.1), array ('name' => 'watermelon ', 'quantity' => 3, 'unit price' => 25.3 )); // Loop 1 for ($ i1 = 0; $ i1
  ';} // Loop 2 for ($ i2 = 0; $ i2
  ';}?>

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.