PHP array copy and Reference Assignment, php array assignment

Source: Internet
Author: User

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.

 


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.