php - while 循环的迭代次数是预期的两倍,而嵌套 for 循环的迭代次数是预期的 1.5 倍

发布于 2024-10-09 22:01:21 字数 1185 浏览 4 评论 0原文

该脚本应该获取多维数组并迭代这些值。

数组大小为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瘫痪情歌 2024-10-16 22:01:21

问题1:

$sizeof = sizeof($games)-1;

解释1:

for($i=0, $sizeof = sizeof($games);$i<=$sizeof;$i++)  

上面会执行11次就是sizeof($games)是10
因此,

for($i=1, $sizeof = sizeof($games);$i<=$sizeof;$i++)  

or

for($i=0, $sizeof=sizeof($games)-1;$i<=$sizeof;$i++)  

问题 2 :

$e = sizeof($games);

解释 2:

$e = count($games);  
...
$e += $e;

如果 $games 的最终大小是 50,则只需将其加起来为 100
所以,这是某种逻辑问题

problem 1:

$sizeof = sizeof($games)-1;

explain 1:

for($i=0, $sizeof = sizeof($games);$i<=$sizeof;$i++)  

the above will execute 11 times is the sizeof($games) is 10
So, either

for($i=1, $sizeof = sizeof($games);$i<=$sizeof;$i++)  

or

for($i=0, $sizeof=sizeof($games)-1;$i<=$sizeof;$i++)  

problem 2 :

$e = sizeof($games);

explain 2 :

$e = count($games);  
...
$e += $e;

If the final size of $games is 50, you just sum it to 100
so, it some kind of logic problem

二智少女 2024-10-16 22:01:21

我知道答案已被接受,但我想我应该重构并使其更干净一些。

function retrieveGamesInfo($limit, $start = 0)
{
  $feedHandler = new FeedHandler(); // ignore this, just for testing to simluate your call

  if ($start > $limit)
    throw new Exception("Start index must be within the limit");

  $result = Array(
    'TotalGames' => 0,
    'TotalFileSize' => 0
  );

  // iterate over the results in groups of 10
  $range = $start;
  while ($range < $limit)
  {
    $range_end = $range + 10; // change me to play with the grab amount
    if ($range_end > $limit)
      $range_end = $limit;

    // grab the next 10 entries
    $games = $feedHandler->getGames($range,$range_end);

    $result['TotalGames'] += count($games);

    foreach ($games as $game)
      $result['TotalFileSize'] += $game['swf_file_size'];

    $range = $range_end;
  }
  return $result;
}
var_dump(retrieveGamesInfo(50));

基于我阅读和吸收的所有内容,这应该是一个很好的补充。上面提供了以下结果:

array(2) {
  ["TotalGames"]=>
  int(50)
  ["TotalFileSize"]=>
  int(275520)
}

I know the answer has been accepted, but thought I'd refactor and make this a little more clean.

function retrieveGamesInfo($limit, $start = 0)
{
  $feedHandler = new FeedHandler(); // ignore this, just for testing to simluate your call

  if ($start > $limit)
    throw new Exception("Start index must be within the limit");

  $result = Array(
    'TotalGames' => 0,
    'TotalFileSize' => 0
  );

  // iterate over the results in groups of 10
  $range = $start;
  while ($range < $limit)
  {
    $range_end = $range + 10; // change me to play with the grab amount
    if ($range_end > $limit)
      $range_end = $limit;

    // grab the next 10 entries
    $games = $feedHandler->getGames($range,$range_end);

    $result['TotalGames'] += count($games);

    foreach ($games as $game)
      $result['TotalFileSize'] += $game['swf_file_size'];

    $range = $range_end;
  }
  return $result;
}
var_dump(retrieveGamesInfo(50));

based one everything I've read and taken in, this should be a good supplement. The above provides the following result:

array(2) {
  ["TotalGames"]=>
  int(50)
  ["TotalFileSize"]=>
  int(275520)
}
我为君王 2024-10-16 22:01:21

正如我在评论中所说, $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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文