Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your second example is still <strong>expected</strong> behaviour... You're working on a <strong><em>copy</em></strong> of the array and its values, not the actual array values, unless you use "by reference"</p> <pre><code>foreach( $arr as $x =&gt; &amp;$line){ if( preg_match("/word$/", $line)){ $line = preg_replace( "/word$/", '', $line); $arr[$x+1] = 'word ' . $arr[$x+1]; } } unset($line); </code></pre> <p>Note the use of the &amp;$line rather than $line, and it's always safest to unset after the loop has finished</p> <p><strong>EDIT</strong></p> <p>Quoting from the <a href="http://www.php.net/manual/en/control-structures.foreach.php" rel="nofollow noreferrer">PHP manual</a>:</p> <blockquote> <p>Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it.</p> </blockquote> <p><strong>EDIT</strong></p> <blockquote> <p>I don't recommend the use of references in a foreach(), it is really slow, in my case it was 16x slower. The solution in to add this line: $line = $arr[$x]; in the beginning of the loop, it seems to do some magick tricks and everything works as I would expect</p> </blockquote> <p>Not really a magic trick. It simply overwrites the value of $line extracted via the foreach loop with $line directly from the array via the key ($x).</p> <p>YMMV, but it doesn't seem much slower to me.</p> <p>The following test script:</p> <pre><code>$arr = range(1,9999); $callStartTime = microtime(true); foreach($arr as &amp;$line) { $line += 1; } unset($line); $callEndTime = microtime(true); $callTime = $callEndTime - $callStartTime; echo '&lt;br /&gt;Call time to access by reference was '.sprintf('%.4f',$callTime)." seconds&lt;br /&gt;\n"; foreach($arr as $x =&gt; &amp;$line) { $line += 1; } unset($line); $callEndTime = microtime(true); $callTime = $callEndTime - $callStartTime; echo '&lt;br /&gt;Call time to access by reference (retrieving key as well) was '.sprintf('%.4f',$callTime)." seconds&lt;br /&gt;\n"; $callStartTime = microtime(true); foreach($arr as $x =&gt; $line) { $arr[$x] += 1; } unset($line); $callEndTime = microtime(true); $callTime = $callEndTime - $callStartTime; echo '&lt;br /&gt;Call time and then access array element directly was '.sprintf('%.4f',$callTime)." seconds&lt;br /&gt;\n"; $callStartTime = microtime(true); foreach(array_keys($arr) as $x) { $arr[$x] += 1; } $callEndTime = microtime(true); $callTime = $callEndTime - $callStartTime; echo '&lt;br /&gt;Call time to access array_keys was '.sprintf('%.4f',$callTime)." seconds&lt;br /&gt;\n"; </code></pre> <p>returns the following timings:</p> <pre><code>Call time to access by reference was 0.0018 seconds Call time to access by reference (retrieving key as well) was 0.0039 seconds Call time to access key and then access array element directly was 0.0077 seconds Call time to access array_keys was 0.0071 seconds </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload