Answer the question
In order to leave comments, you need to log in
PHP7 and foreach. Insidious change?
As the documentation says, in PHP7 foreach now operates on a copy of the array rather than on the array itself.
That is, this code used to work, but now it doesn't (if I'm not mistaken):
<?php
foreach ($arr as $k=>$v)
{
if ($some_condition==$v) {
unset($arr[$k]);
}
}
Answer the question
In order to leave comments, you need to log in
Usually, unsuitable members of the old array are not removed, but a new one is created from suitable ones.
$properElements = [];
foreach ($arr as $k => $v)
{
if ($some_condition != $v) {
$properElements[] = $v;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question