A question about php reference in array

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($...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-12-2008
hi and hello
 
Posts: n/a
Default A question about php reference in array

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?

Reply With Quote
  #2 (permalink)  
Old 05-12-2008
Piotr
 
Posts: n/a
Default Re: A question about php reference in array

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
Reply With Quote
  #3 (permalink)  
Old 05-14-2008
hi and hello
 
Posts: n/a
Default Re: A question about php reference in array

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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:16 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0