简体   繁体   中英

php - while loop is itering double the times expected and nested for loop is iterating 1.5 times more than expected

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>';

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

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)
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM