This is a discussion on A question about php reference in array within the PHP Language forums, part of the PHP Programming Forums category; a code segment: $i = array('data'=>1000); $arr = array('i'=>&$i); $arr2 = $arr; $i['data'] = 200; print_r($...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
a code segment:
$i = array('data'=>1000); $arr = array('i'=>&$i); $arr2 = $arr; $i['data'] = 200; print_r($arr); print_r($arr2); the result is: Array ( [i] => Array ( [data] => 200 ) ) Array ( [i] => Array ( [data] => 200 ) ) This means the change of variable $i effects the $arr2 which is a copy of $arr. Some one knows? |
|
|||
|
hi and hello wrote:
> $i = array('data'=>1000); Here you have created that array > $arr = array('i'=>&$i); Here you said something like: create an array with a field named 'i', and let the field contain a link to $i variable > $arr2 = $arr; Here you have made a copy of the array, like the above one, it has the 'i' field, and the field contains the same link It's like a link in your file system, when you copy a link, it does not copy the original file. > $i['data'] = 200; Here you changed the value, it;s lieke you would change what the file (in the file system) contains When you print the arrays, PHP searched for the $i variable, that is referenced by both tables and prints the reference target value. That is what the reference is ment to do. You can find more here: php.net/manual/pl/language.references.php Check out the examples in comments given by people. best regards Piotr N |
|
|||
|
On May 13, 1:41 am, Piotr <s...@poczta.onet.pl> wrote:
> hi and hello wrote: > > $i = array('data'=>1000); > > Here you have created that array > > > $arr = array('i'=>&$i); > > Here you said something like: > create an array with a field named 'i', and let > the field contain a link to $i variable > > > $arr2 = $arr; > > Here you have made a copy of the array, > like the above one, it has the 'i' field, > and the field contains the same link > > It's like a link in your file system, when you copy a link, > it does not copy the original file. > > > $i['data'] = 200; > > Here you changed the value, it;s lieke you would change > what the file (in the file system) contains > > When you print the arrays, PHP searched for the $i variable, > that is referenced by both tables and prints the reference target value. > That is what the reference is ment to do. > > You can find more here: > php.net/manual/pl/language.references.php > Check out the examples in comments given by people. > > best regards > Piotr N Thanks for your excellent explaination. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|