 <p>You could use <code>array_map</code> instead of loops and pass-by-reference. You avoid mutation this way. For example:</p> <pre><code>$add = function($x) { return function($y) use($x) { return $x + $y; }; }; $firstArray = array_map($add(3), $firstArray); $secondArray = array_map($add(7), $secondArray); </code></pre>