Problems w/accessing a PHP array by reference

I'm not sure yet if this is a bug or a feature of PHP, but be very careful when mixing arrays with the PHP reference operator (&). I just spent many hours tracking down a bug where changing an array copy was also changing the original array. It took several more hours to figure out that accessing an array by reference caused subsequent copies of that array to also be linked by reference!

Here's a simple piece of PHP that illustrates what I found (PHP Version 5.1.6):

This code copies an array and changes one of it's expected the original array doesn't change:

$array1 = array(1,2);
$array2 = $array1;
$array2[1]=22; // Change one value, $array1 remains unchanged


[0] => 1
[1] => 2

Read more: Problems w/accessing a PHP array by reference