php - while 循环的迭代次数是预期的两倍,而嵌套 for 循环的迭代次数是预期的 1.5 倍
该脚本应该获取多维数组并迭代这些值。
数组大小为 10,每个元素应包含一个关联数组:
$games[0] => array('foo' => 'bar')
$games[1] => array('foo1' => 'bar1')
etc..
在此示例中,while 循环应迭代 5 次。 for 循环应为 while 循环的每次迭代迭代 10 次。
所以我期待回声是:
countwhile = 5 countfor = 50 totalgames = 50
但我实际上认为
countwhile = 5 countfor = 150 totalgames = 150
我相信 $games 数组不是问题,因为我之前已经在下面进行过调用并使用 print_r 来查看内容,并且它符合预期。
整个代码并不像我的 index.php 页面上那样位于函数或类中,问题可能与变量范围有关吗?
$totalruns = 5;
$endindx = 10;
$startindx = 0;
$countwhile = 0;
$countfor = 0;
$totalfilesize = 0;
$totalgames = 0;
$sizeof = 0;
while($totalruns > 0)
{
$games = $feedHandler->getGames($startindx, $endindx);
$sizeof = sizeof($games);
for($i=0; $i<$sizeof; $i++)
{
$totalfilesize += $games[$i]['swf_file_size'];
$countfor++;
}
$startindx += 10;
$endindx += 10;
$totalruns -= 1;
$totalgames += $sizeof;
unset($games);
}
echo'<p>' . ' countwhile = ' . $countwhile . ' countfor = ' . $countfor . '</p>';
This script is supposed to to get a multidimensional array and iterate through the values.
The array size is 10 and each element should contain an associative array:
$games[0] => array('foo' => 'bar')
$games[1] => array('foo1' => 'bar1')
etc..
The while loop should iterate 5 times in this example. The for loop should iterate 10 times for each iteration of the while loop.
So I am expecting the echo to be:
countwhile = 5 countfor = 50 totalgames = 50
but im actually getting
countwhile = 5 countfor = 150 totalgames = 150
I believe $games array is not the problem because i have have made that call below before and have used print_r to view the contents and it is as expected.
This whole code is not in a function or class just as is on my index.php page, could the problem be to do with the variable scopes?
$totalruns = 5;
$endindx = 10;
$startindx = 0;
$countwhile = 0;
$countfor = 0;
$totalfilesize = 0;
$totalgames = 0;
$sizeof = 0;
while($totalruns > 0)
{
$games = $feedHandler->getGames($startindx, $endindx);
$sizeof = sizeof($games);
for($i=0; $i<$sizeof; $i++)
{
$totalfilesize += $games[$i]['swf_file_size'];
$countfor++;
}
$startindx += 10;
$endindx += 10;
$totalruns -= 1;
$totalgames += $sizeof;
unset($games);
}
echo'<p>' . ' countwhile = ' . $countwhile . ' countfor = ' . $countfor . '</p>';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题1:
解释1:
上面会执行11次就是
sizeof($games)
是10因此,
问题 2 :
解释 2:
如果
$games
的最终大小是 50,则只需将其加起来为 100所以,这是某种逻辑问题
problem 1:
explain 1:
the above will execute 11 times is the
sizeof($games)
is 10So, either
problem 2 :
explain 2 :
If the final size of
$games
is 50, you just sum it to 100so, it some kind of logic problem
我知道答案已被接受,但我想我应该重构并使其更干净一些。
基于我阅读和吸收的所有内容,这应该是一个很好的补充。上面提供了以下结果:
I know the answer has been accepted, but thought I'd refactor and make this a little more clean.
based one everything I've read and taken in, this should be a good supplement. The above provides the following result:
正如我在评论中所说, $e 在每个循环中都会被覆盖,因此最后 $e 中的内容只是 $games *2 中元素的最后计数。
添加了 ajreal 问题,这意味着结果是您的代码预期呈现的:-)并且我很确定您的最后一个 $game 不只是 10 个元素,而是 50 个。当然很安静......但它很难阅读。
As i said in my comment $e is overwritten at each loop, so what you have in $e at the end is just the last count of elements in $games *2.
Added with ajreal issues this means results are what your code is expected to render :-) and I'm quite sure your last $game is not just 10 elements but 50. Quiet sure... but it's hard to read.