PHP array copy and Reference Assignment, php array assignment
1. When the PHP array variable $ arr is assigned to another variable $ one, the array variable $ arr is copied to $ one, even if $ arr is a multi-dimensional array.
Example:
$ Arr = array (1, 2, 3, array ('one', 'two'); $ one = $ arr;
# Output the original array $ arrprint_r ($ arr ); # Array ([0] => 1 [1] => 2 [2] => 3 [3] => Array ([0] => one [1] => two ))
# Output new array $ one
Print_r ($ one ); # Array ([0] => 1 [1] => 2 [2] => 3 [3] => Array ([0] => one [1] => two ))
# Modify the original array $ arr and output $ arr [0] = 4; $ arr [3] [1] = 'three '; print_r ($ arr ); # Array ([0] => 4 [1] => 2 [2] => 3 [3] => Array ([0] => one [1] => three ))
# Output a new array $ oneprint_r ($ one ); # Array ([0] => 1 [1] => 2 [2] => 3 [3] => Array ([0] => one [1] => two ))
The example shows that modifying the value of the original array does not affect the new array.
2. When the PHP array variable $ arr assigns a reference value to another variable $ one, $ arr does not copy the array variable $ one, instead, assign the array reference to $ one, that is, two variables point to the same array.
Example:
$ Arr = array (1, 2, 3, array ('one', 'two'); $ one = & $ arr; # output the original array $ arrprint_r ($ arr ); # Array ([0] => 1 [1] => 2 [2] => 3 [3] => Array ([0] => one [1] => two )) # output a new array $ oneprint_r ($ one ); # Array ([0] => 1 [1] => 2 [2] => 3 [3] => Array ([0] => one [1] => two )) # modify the original array $ arr and output $ arr [0] = 4; $ arr [3] [1] = 'three '; print_r ($ arr ); # Array ([0] => 4 [1] => 2 [2] => 3 [3] => Array ([0] => one [1] => three )) # output a new array $ oneprint_r ($ one ); # Array ([0] => 4 [1] => 2 [2] => 3 [3] => Array ([0] => one [1] => three ))
The example shows that the value of the original array is modified, and the value of the new array is also changed.
The above code is written directly when you write a blog. If you copy and paste the code to sublime text3, an error is returned.
After half a day of doubt, I found that there was space in the code format. It may be because this blog plug-in is used for direct writing. Modify the format in sublime text3 and then run normally.