【发布时间】:2025-12-22 17:10:18
【问题描述】:
如何阻止嵌套的 foreach 循环重置内部数组指针 例如
foreach ($example as $example2)
{
foreach ($xample as $xample2);
{
}
}
例如,如果 $example & $xample 包含数组 (1,2,3)
我想做这样的事情
foreach ($example as $example2)
{
do something with array[1]
foreach ($xample as $xample2);
{
do something else with array[1]
}
}
然后继续 2,3 等... 希望我的问题很清楚,因为据我了解,内部数组指针在第二次循环期间被重置http://us2.php.net/manual/en/control-structures.foreach.php
编辑 所以这就是现在正在发生的事情 假设 $example 包含以下值 1,2,3 & $xample 包含相同的值
foreach ($example as $example2)
{
echo ($example2)
foreach $xample as xample2)
{
echo ($xample2)
}
}
输出看起来像这样,第一个循环输出 1,2,3,第二个循环只输出 1,1,1 我想要的是,第一个循环 1,2,3 第二个循环 1,2,3
编辑 2 代码粘贴在这里http://codepad.org/r1py8HR5 输出显示有 5 个 $examples 和 7 个 $xample 被回显 &xample 包含相同的 $fname2 7 次
【问题讨论】:
-
数组[1]是什么意思?
-
我的意思是如果数组[某个值 1,某个值 2,] 等。
-
如果您愿意,可以在codepad.org 创建一个简单的工作示例 :)