473,597 Members | 2,256 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question about php reference in array

a code segment:
$i = array('data'=>1 000);

$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 1335
hi and hello wrote:
> $i = array('data'=>1 000);
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.refere nces.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.on et.plwrote:
hi and hello wrote:
$i = array('data'=>1 000);

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.refere nces.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
3791
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
1555
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 produced by Regex. The Collection's entries are all valid strings. If I CopyTo to a string array it compiles ok, but fails at run time with an InvalidCastException. It works if I CopyTo an object array. ... does this mean that the MatchCollection's...
5
2090
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 object, is still created on the stack, I would say, so I don't understand this inline business. Apart from the fact that it is my understanding that "inline" as it exists in C++ doesn't exist in C#. Could someone please shed some light on this...
4
2956
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 Product class and from two interfaces called Wearable and Rentable. These classes has the following names Book, Soda, Shoe, Coffee, BubbleGum and Underwear.
9
2015
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 used to using Arrays so I would normally do something like this: Set an array up during the init routine (called from form_load) say of
12
1310
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
2435
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 a "reference"??As I pass the array by value, the callee can change that array. However,when I use ref, the callee and caller point to two different arrays. So could anyone please explain the 2 different situations?
14
1764
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, but I purposely had this array filled unsequentially because the key for the array is meant to act as an ID which has a contextual meaning in my script. The problem, of course, is that for...in also returns my method 'contains' as one of the...
0
921
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 remove this array. If you for example set vektor = null then you give the GC permission to remove this array.
8
2206
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
7962
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8267
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5844
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3921
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2394
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.