tags:

views:

151

answers:

3
foreach ($a as $b)
{
do function
}
+1  A: 
$i = 0;
while ($i < count($a))
{
  $b = $a[$i];

  //do function

  $i++;
}
luvieere
Good, but the count might not be available for all enumerations.
uosɐſ
also, this assumes that keys are integers from 0 to the count - 1, which is not necessarily the case
newacct
It would also be more efficient to place the count() outside of the loop to prevent recounting the array every single time.
BraedenP
The count() is only executed once, that's how the while loop works.
luvieere
@luvieere: a while loop executes its test at the beginning of each iteration through the loop. In your example, `count($a)` will be executed multiple times. If you want to test this, try `function mycount($a) { echo 'test'; return count($a); }` and then `while ($i < mycount($a))`
gnarf
+5  A: 
while ($b.MoveNext())
{
    $a = $b.Current;
}
uosɐſ
you mean `->` instead `.` right? given that was tagged as *php*
Gabriel Sosa
+5  A: 
reset($a);
while (list($key, $value) = each($a)) { 
  //...
}

$keys = array_keys($a);
while (($key = array_shift($keys)) !== NULL))
{
  $b = $a[$key];
}
gnarf