foreach($tests as $i => $test){
if ($test->getNumber() == 2) {
echo $i . $getName() ;
} else {
$i--;
}
}
for example:
number | name
1 | aaa
2 | bbb
1 | ccc
2 | vvv
2 | ccc
show me:
$i $name
2 bbb
4 vvv
5 ccc
instead of:
$i $name
1 bbb
2 vvv
3 ccc
how can i fix it?
Beacuse foreach is returning you the index of $test inside $tests array so your $tests array is:
$i which is rewritten each iteartion by foreach statement.
Try this (using another counter):