Home Backend Development PHP Tutorial 使用foreach循环二维数组后的有关问题

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

Jun 13, 2016 pm 01:45 PM
array echo foreach gt

使用foreach循环二维数组后的问题
循环二次的话,第二次循环就不显示,但是如果注释掉第一次的话,第二次循环的内容就可以正常显示,
哪位朋友知道原因的请告诉一下,非常感谢!


PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
    $products2 = array(
     array('名称' =&gt; '苹果','数量' =&gt; 6,'单价' =&gt; 12.2),
     array('名称' =&gt; '香蕉','数量' =&gt; 2,'单价' =&gt; 15.1),
     array('名称' =&gt; '西瓜','数量' =&gt; 3,'单价' =&gt; 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('名称' => '苹果','数量' =&gt; 6,'单价' =&gt; 12.2),
     array('名称' =&gt; '香蕉','数量' =&gt; 2,'单价' =&gt; 15.1),
     array('名称' =&gt; '西瓜','数量' =&gt; 3,'单价' =&gt; 25.3)
    );

//循环一
    for ($i1=0;$i1<count foreach as echo reset></count>';
    }

//循环二
       for ($i2=0;$i2<count while each echo></count>';
    }


?&gt; <div class="clear">
                 
              
              
        
            </div>
Copy after login
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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

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? What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? Apr 27, 2023 pm 03:40 PM

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 How to determine the number of foreach loop in php Jul 10, 2023 pm 02:18 PM

How to determine the number of foreach loop in php

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Fix: Snipping tool not working in Windows 11

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

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

Go language development essentials: 5 popular framework recommendations Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

Go language development essentials: 5 popular framework recommendations

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Sort array using Array.Sort function in C#

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

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

See all articles