473,395 Members | 1,677 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,395 software developers and data experts.

using $array1 = $array2 in PHP5 as if array is an object?

I wonder in PHP5, can we use

$array1 = $array2 as if it is a class?

i think right now, the whole array is copied (by key and value)...

and i want to assign by reference but NOT in the alias sense.

in other world, i want to say

$array1 =& $array2;
$array2 = range(1, 3);

and I don't want $array1 to change, just like any other objects in
PHP5.

Sep 27 '07 #1
2 2670

"Summercool" <Su************@gmail.comwrote in message
news:11**********************@w3g2000hsg.googlegro ups.com...
>I wonder in PHP5, can we use

$array1 = $array2 as if it is a class?

i think right now, the whole array is copied (by key and value)...

and i want to assign by reference but NOT in the alias sense.

in other world, i want to say

$array1 =& $array2;
$array2 = range(1, 3);

and I don't want $array1 to change, just like any other objects in
PHP5.
not sure i follow. the above code will produce *exactly* what you said you
don't want to. have you answered your own question by writing the 8 lines of
code it would take?

$array1 = $array2;
$array2 = range(1, 3);
echo '<precopy ' . $array1 . '</pre>';
unset($array1);
unset($array2);
$array1 = &$array2;
$array2 = range(1, 3);
echo '<prereference ' . $array1 . '</pre>';

that's all you get in any version of php currently. you get a copy of data
at a new address, or a pointer to an address where the data can be found
(c++ explanation...i know what aliasing is in php...long discussion on that
this week ;^)

what behavior are you wanting that the first echo in the example doesn't
provide? describe this 'reference'.
Sep 27 '07 #2
Summercool wrote:
I wonder in PHP5, can we use

$array1 = $array2 as if it is a class?

i think right now, the whole array is copied (by key and value)...

Correct. Arrays, like everything else, are assigned by copy, however php
implements lazy copying, that is, no bytes are physically moved until
you modify one of the copies.

$a1 = array(1, 2, 3);
$a2 = $a1; // no copying
$a2[0] = 9; // copy $a1 to $a2 and change $a2
>
and i want to assign by reference but NOT in the alias sense.

in other world, i want to say

$array1 =& $array2;
$array2 = range(1, 3);

and I don't want $array1 to change, just like any other objects in
PHP5.
Wrap them in objects.

//

$a1 = new stdclass;
$a1->a = array(1, 2, 3);

$a2 = $a1;

$a1->a[0] = 123;
echo $a2->a[0]; // $a1->a and $a2->a are the same

$a2 = 'foo';
var_dump($a1); // $a1 and $a2 are NOT the same

//
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Sep 27 '07 #3

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

Similar topics

8
by: Hal Vaughan | last post by:
I may have my terms mixed up (I'm not a professional programmer), but if I do this: var array1 = new Array("TestOne", "TestTwo", "TestThree", "TestFour"); var array2 = new Array(); var test1...
4
by: JR | last post by:
Hey all, I am passing a two dimensional array to a function. It basically looks like this int test(double array2 , const CONST1, const CONST2) { int returnValue; for (int i = 0;...
6
by: shookim | last post by:
The correct syntax for an array literal would be: this.managers ; (which works) My questioin is, how do you recreate this same syntax pulling from a recordset containing over 3500 records...
1
by: conor.robinson | last post by:
Using large arrays of data I found it is MUCH faster to cast arrays to matricies and then multiply the two matricies togther (scipy.matrix(ARRAY1)*scipy.matrix(ARRAY2)) in order to do a matrix...
3
by: Ian Davies | last post by:
I have a table with each column containing the following form objects - combobox, hidden field, textarea, and tick box. I have created arrays from each of these columns and now I wish to use the...
8
by: padew | last post by:
I want make a vfariabl on the fly so: for (var i = 0; i<2; i++) { z+i=new Array("a"); fo to have: z0=a z1=a z1=a
15
by: Gustaf | last post by:
Using VS 2005. I got an 'IpForm' class and an 'IpFormCollection' class, containing IpForm objects. To iterate through IpFrom objects with foreach, the class is implemented as such: public class...
9
by: Bill Grigg | last post by:
All, Can anyone supply an example or reference to an example of using reflection to determine the data types and array lengths contained in a nested stucture in C#? Actually, it is a structure...
15
by: Maarten | last post by:
I've found some answers to my problem on this forum, but not exactly the answer I was looking for. Sorry if I've missed something. This is my situation: I am trying to make an insertion into an...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.