Deep copy and reference assignment of PHP arrays

Source: Internet
Author: User

First, when the PHP array variable $arr assigned to another variable $one, this is the array variable $arr the entire copy to $one, even if $arr is a multidimensional array.

Cases:

$arr=Array(1, 2, 3,Array(' One ', ' both '));$one=$arr;
#output original Array $arrPrint_r($arr);#Array ([0] = 1 [1] = 2 [2] = + 3 [3] = = Array ([0] = = one [1] = both))

#output New Array $one
Print_r($one);#Array ([0] = 1 [1] = 2 [2] = + 3 [3] = = Array ([0] = = one [1] = both))

#modifies the original array $arr, and outputs$arr[0] = 4; $arr[3] [1] = ' three ';Print_r($arr); #Array ([0] = 4 [1] = 2 [2] = 3 [3] = = Array ([0] = = one [1] = three))

#output new Array $onePrint_r($one); #Array ([0] = 1 [1] = 2 [2] = + 3 [3] = = Array ([0] = = one [1] = both))
As can be seen from the example, modifying the value of the original array does not affect the new array

When the PHP array variable $arr uses a reference to assign to another variable $one, the array variable $arr is not copied to the $one, but the reference to the array is assigned to $one, that is, two variables point to the same array
Cases:
$arr=Array(1, 2, 3,Array(' One ', ' both '));$one= &$arr;#output original Array $arrPrint_r($arr);#Array ([0] = 1 [1] = 2 [2] = + 3 [3] = = Array ([0] = = one [1] = both))#output new Array $onePrint_r($one);#Array ([0] = 1 [1] = 2 [2] = + 3 [3] = = Array ([0] = = one [1] = both))#modifies the original array $arr, and outputs$arr[0] = 4; $arr[3] [1] = ' three ';Print_r($arr);#Array ([0] = 4 [1] = 2 [2] = 3 [3] = = Array ([0] = = one [1] = three))#output new Array $onePrint_r($one);#Array ([0] = 4 [1] = 2 [2] = 3 [3] = = Array ([0] = = one [1] = three))
As can be seen from the example, the value of the original array is modified, and the new array value is changed.

The above code, is written directly in the blog, if directly copied paste into the sublime text3 inside, execution will error.

Puzzled half a day, only to find that the code format has a space reason. Probably because it is written directly with this blog plugin. It works correctly after you modify the format in sublime Text3.



Deep copy and reference assignment of PHP arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.