473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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?

Jun 2 '08 #1
2 1323
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
Jun 2 '08 #2
On May 13, 1:41 am, Piotr <s...@poczta.onet.plwrote:
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.
Jun 2 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
3
by: Barry Mossman | last post by:
Hi, I get the feeling that I am missing something with regards to casting. The CopyTo method allows me to copy the contents of a collection into an array. My collection is a MatchCollection...
5
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that...
4
by: Tony Johansson | last post by:
Hello! Assume we have the following classes and interfaces. We have a class called Inventory that is associated to a class called Product. We have some classes that is derived from this...
9
by: me | last post by:
Hi All, I am new to Classes and learniing the ropes with VB.NET express Here's my question - say I have a want to manage a list of books. Each book has an Author, Title and ISBN Now, I am...
12
by: Gilbert | last post by:
H, i'm starting with asp.net/vb.net and have some questions about arrays and collections: 1) what's the difference between: dim x() as string and dim x as array
9
by: Jack | last post by:
If I don't specify "ref" in the argument list when passing an array to the callee, I am passing the array (reference) by value. But this makes me confused because it actually means a "reference" of...
14
by: ablock | last post by:
I have an array to which i have a added a method called contains. I would like to transverse this array using for...in...I understand fully that for...in is really meant for Objects and not Arrays,...
0
by: TonyJ | last post by:
Hello! You can write int vektor = new int; This array will be stored on the heap and you will have a reference(vektor) to this array. As long as you keep a reference to this array will GC never...
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.