9

Is it possible to increment a php variable inside a foreach? I know how to loop by declaring outside.

I'm looking for something like the syntax below

foreach ($some as $somekey=>$someval; $i++)
{

}
gvm
  • 1,153
  • 3
  • 12
  • 19
  • The above is not valid. `$i++` is not allowed within the `foreach`. Did you mix it up with `for($i = 0; $i < 10; $i++)` ? – Betaminos Jun 15 '12 at 09:42
  • s, I 'm looking for something similar. It can save me two lines of codes – gvm Jun 15 '12 at 09:49

10 Answers10

47

No, you will have to use

$i = 0;
foreach ($some as $somekey=>$someval) {
    //xyz
    $i++;
}
Ananth
  • 4,227
  • 2
  • 20
  • 26
  • 2
    Beware if you have a `continue` statement in the loop (which could skip the increment) - in that case it might be better to start with `$i = -1` and increment it before `//xyz`. – Jake May 01 '20 at 14:53
5
foreach ($some as $somekey=>$someval)
{
     $i++;


}
Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
2

lazy way of doing is:

{
    $i=1;
    foreach( $rows as $row){
        $i+=1;
    }
}

,but you have to scope foreach for $i to dont exist after foreach or at last unset it

$i=1;
foreach( $rows as $row){
    $i+=1;
}
unset($i);

, but you should use for cycle for that as leopold wrote.

Matas Lesinskas
  • 414
  • 6
  • 13
1
$dataArray = array();
$i = 0;
foreach($_POST as $key => $data) {
    if (!empty($data['features'])) {
        $dataArray[$i]['feature'] = $data['features'];
        $dataArray[$i]['top_id'] = $data['top_id'];
        $dataArray[$i]['pro_id'] = $data['pro_id'];
    }
    $i++;
}
Thilina Sampath
  • 3,615
  • 6
  • 39
  • 65
Majid Mushtaq
  • 319
  • 1
  • 3
  • 14
1

I know it is an old one here, but these are my thoughts to it.

$some = ['foo', 'bar'];

for($i = 0; $i < count($some); $i++){
 echo $some[$i];
}

-- Update --

$some = ['foo', 'bar'];
$someCounted = count($some);

for($i = 0; $i < $someCounted; $i++){
 echo $some[$i];
}

It would achieve, what you are looking for in first place.
Yet you'd have to increment your index $i.
So it would not save you any typing.

leopold
  • 1,971
  • 1
  • 19
  • 22
  • 1
    using count inside for will execute count function every loop, dont use like that, write before loop to variable and use it later. – Matas Lesinskas Oct 17 '17 at 12:05
0

Is there any reason not to use

foreach ($some as $somekey=>$someval)
{
$i++;
}

?

axiomer
  • 2,088
  • 1
  • 17
  • 26
0
foreach ($some as $somekey=>$someval)
{
    $i++;
}
Nadir Sampaoli
  • 5,437
  • 4
  • 22
  • 32
0
foreach ($some as $somekey=>$someval)
{
 $i++;
}

i is just a variable. Even though it's used to iterate over whatever item you're using, it can still be modified like any other.

YYZ
  • 749
  • 4
  • 8
  • 18
0

This will do the trick! Remember that you'll have to define $i = 0 before the foreach loop if you want to start counting/incrementing from 0.

$i = 0;
foreach ($some as $somekey=>$someval) {
    $i++;
}
minethisis
  • 11
  • 2
0

Unfortunately, this is not possible. I was looking for an elegant way to do this, very similar to this:

for ($k = 0 ; $k < $columns; $k++){ echo '<td></td>'; }

This works for the for functionality in PHP, but then you would have to pull the item, such as $columns[$k], and then you're back to a less than ideal situation.

Reference