开发者

foreach returns more elements than count()

开发者 https://www.devze.com 2023-01-08 20:20 出处:网络
I\'m using Symfony 1.2.7 and Doctrine 1.1. I have $activities (sfOutputEscaperIteratorDecorator - Doctrine_Collection). I\'m escaping everything on settings.yml with ESC_SPECIALCHARS method. If I were

I'm using Symfony 1.2.7 and Doctrine 1.1. I have $activities (sfOutputEscaperIteratorDecorator - Doctrine_Collection). I'm escaping everything on settings.yml with ESC_SPECIALCHARS method. If I weren't escaping it, it would work without any problem, so I think the problem is related with sfOutputEscaperIteratorDecorator.

If I do echo count($activities) it returns me 5

I re开发者_如何学运维move several elements:

foreach($activities as $key => $a){
  if(...){
    $activities->remove($key);
  }
}

Then if I do echo count($activities) it returns me 2

However when I iterate through the elements, I still have the same 5 elements:

foreach($activities as $activity){
  ..
}

Any idea?

thanks!


Try this instead:

foreach($activities as $key => $a){
  if(...){
    unset($activitie[$key]);
  }
}


There's also array_splice()

0

精彩评论

暂无评论...
验证码 换一张
取 消