PHP 7 changes to foreach: Can I still delete items in they array on which I'm iterating?
<p>The <a href="http://php.net/manual/en/migration70.incompatible.php" rel="noreferrer">PHP 7 Backward-Incompatible Changes Document</a> says the following about foreach:</p>

<blockquote>
  <p>When used in the default by-value mode, <code>foreach</code> will now operate on a copy of the array being iterated rather than the array itself. This means that changes to the array made during iteration will not affect the values that are iterated.</p>
</blockquote>

<p>I'm trying to understand what this means, and my main question is whether this code will work the same in PHP 7 as it does in PHP 5.6?</p>

<pre><code>foreach($array as $elementKey =&gt; $element) {
    if ($element == 'x') {
        unset($array[$elementKey]);
    }
}
</code></pre>

<p>My two questions are:</p>

<ol>
<li><p>Will this code still work?</p></li>
<li><p>If so, can you explain (maybe by example) what this new change in PHP 7 means?</p></li>
</ol>

<p><strong>Edit</strong></p>

<p>I've been re-reading the doc statement. I'm thinking that what it means is that, if you change values of items lower down in the array, those changes won't be there when you get to those items in the iteration. Example:</p>

<pre><code>$array = ['x', 'y', 'z'];

$new = [];

foreach($array as $element) {
    if ($element == 'x') {
        $array[2] = 'a';
    }

    $new[] = $element;
}

print_r($new);
</code></pre>

<p>However, <strong>when I <a href="https://3v4l.org/X69FW" rel="noreferrer">run this example</a> it doesn't seem to show any difference in PHP versions</strong> (although I've never used this tool before, so I'm not sure how it works).</p>

<p>I realize that if I do it by reference, I'll get an <code>a</code> in new. Otherwise I won't. But this seems to be the case in both versions.</p>

<p>What I really need to know is <strong>what is the incompatibility (by example)?</strong></p>

<p><strong>Edit 2</strong></p>

<p>The <a href="https://stackoverflow.com/questions/10057671/how-does-foreach-actually-work/14854568#14854568">answer link suggested</a> by @NikiC provides the rest of the story I was looking for:</p>

<blockquote>
  <p>In most cases this change is transparent and has no other effect than better performance. However there is one occasion where it results in different behavior, namely the case where the array was a reference beforehand:</p>
</blockquote>

<pre><code>$array = [1, 2, 3, 4, 5];
$ref = &amp;$array;
foreach ($array as $val) {
    var_dump($val);
    $array[2] = 0;
}
/* Old output: 1, 2, 0, 4, 5 */
/* New output: 1, 2, 3, 4, 5 */
</code></pre>

<blockquote>
  <p>Previously by-value iteration of reference-arrays was special cases. In this case no duplication occurred, so all modifications of the array during iteration would be reflected by the loop. In PHP 7 this special case is gone: A by-value iteration of an array will always keep working on the original elements, disregarding any modifications during the loop.</p>
</blockquote>

<p>This answer explains the rare "special case" where things work different between versions in regarding <code>foreach</code> operating on a copy of the array.</p>