使用foreach循环二维数组后的有关问题
Jun 13, 2016 pm 01:45 PM
使用foreach循环二维数组后的问题
循环二次的话,第二次循环就不显示,但是如果注释掉第一次的话,第二次循环的内容就可以正常显示,
哪位朋友知道原因的请告诉一下,非常感谢!
- PHP code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> $products2 = array( array('名称' => '苹果','数量' => 6,'单价' => 12.2), array('名称' => '香蕉','数量' => 2,'单价' => 15.1), array('名称' => '西瓜','数量' => 3,'单价' => 25.3) ); //循环一 for ($i1=0;$i1<count foreach as echo></count>'; } //循环二 // reset($products2); 就算加上重设指针,下面的循环还是显示不出来,但是如果把循环一注释掉的话,循环二就能正常显示了,哪位朋友知道原因的请告诉一下,谢谢了! for ($i2=0;$i2<count while each echo></count>'; }
在线等,先谢谢各位了!
------解决方案--------------------
这个可能要子数组的指针也可重置。
for 没有指针的问题,
foreach,可能会移动指针,具体测测吧
each肯定是移动指针的。
你可以用全部用for, foreach,each分别测下看看。
------解决方案--------------------
你第一轮遍历是针对$products2[$i1],而不是$products2。换句话说该reset()的是$products2[$i1],$products2的指针压根就没变动过
------解决方案--------------------
- PHP code
<?php $products2 = array( array('名称' => '苹果','数量' => 6,'单价' => 12.2), array('名称' => '香蕉','数量' => 2,'单价' => 15.1), array('名称' => '西瓜','数量' => 3,'单价' => 25.3) ); //循环一 for ($i1=0;$i1<count foreach as echo reset></count>'; } //循环二 for ($i2=0;$i2<count while each echo></count>'; } ?> <div class="clear"> </div>

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the differences between Huawei GT3 Pro and GT4?

What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList?

How to determine the number of foreach loop in php

Fix: Snipping tool not working in Windows 11

Five selected Go language open source projects to take you to explore the technology world

Go language development essentials: 5 popular framework recommendations

Sort array using Array.Sort function in C#

How to Fix Can't Connect to App Store Error on iPhone
