使用foreach循环二维数组后的有关问题

WBOY
Release: 2016-06-13 13:45:49
Original
862 people have browsed it

使用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>';
    }


Copy after login


在线等,先谢谢各位了!

------解决方案--------------------
这个可能要子数组的指针也可重置。

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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!