Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArrayHash: Unset of key in foreach at on php7 skip some items #103

Closed
norbe opened this issue Mar 18, 2016 · 4 comments
Closed

ArrayHash: Unset of key in foreach at on php7 skip some items #103

norbe opened this issue Mar 18, 2016 · 4 comments

Comments

@norbe
Copy link

@norbe norbe commented Mar 18, 2016

$hash = \Nette\Utils\ArrayHash::from([1 => 1, 2=> 2, 3=> 3]);
foreach($hash as $key => $value) {
    unset($hash[$key]);
}
dump($hash);

Output is:

Nette\Utils\ArrayHash #3f75
2 => 2

I'm not sure, is it bug (in php?) or it's just not supported and I'm using it wrong?

@dg

This comment has been minimized.

Copy link
Member

@dg dg commented Mar 18, 2016

Oh yeah, people today are so lazy to simplify example and search on bugs.php.net. Ten minutes of work: https://bugs.php.net/bug.php?id=70246. IMHO php bug.

@JanTvrdik

This comment has been minimized.

Copy link
Contributor

@JanTvrdik JanTvrdik commented Mar 18, 2016

I'm not sure this is a bug – see my comment on php.net.

  • Calling unset advances iterator – seems OK to me, what else could it do? Going backwards is generally not possible (there is no Iterator::prev)
  • Foreach must respect iterator position and continue from there.

It's just a sideeffect of using external iterator which the foreach has only partial control of.

@dg

This comment has been minimized.

Copy link
Member

@dg dg commented Mar 18, 2016

Maybe BC break, I do not care.

@dg dg closed this in e8749e5 Mar 18, 2016
@norbe

This comment has been minimized.

Copy link
Author

@norbe norbe commented Mar 18, 2016

Thanks for quick fix. In future, I will try to search better before I post issue here (I have been used google only and did not found anythink relevant).

dg added a commit that referenced this issue Mar 18, 2016
dg added a commit that referenced this issue Mar 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.