metawidget: A platypus looking pensive. (Default)
[personal profile] metawidget
How ugly is this approach to making a copy of an array that moves only values, not references?

$copy_of_array = unserialize(serialize($multidimensional_array));

I feel a little dirty... assuming I'm not too worried about efficiency, should I?

Date: 2006-08-21 04:34 am (UTC)
From: [identity profile] http://users.livejournal.com/hub_/
well it is PHP. The language wihtout design.

Even Perl sound sane compared to PHP.

(sorry I don't have a real answer)

Date: 2006-08-21 05:19 am (UTC)
From: [identity profile] v0idnull.livejournal.com
$a2 = $a1
$a2 =& $a1

is not the same as $a1 =& $a2;

The first, you make a copy. $a2 is a brand new variable in the table.
The second, $a2 is a reference to the $a1 symbol.

What you're doing, is the same thing as $a2 = $a1. Serialize just makes a textual representation of a variable. when you unserialize a variable, you get an identical copy, just like doing $a2 = $a1.

Date: 2006-08-21 01:00 pm (UTC)
From: [identity profile] v0idnull.livejournal.com

$a1[0] = 'hello world';
$a2 = $a1;
$a2[0] = 'foo bar';
echo $a1[0].':'.$a2[0]; // hello world:foo bar

$a2 =& $a1;
$a2[0] = 'foo bar';
echo $a1[0].':'.$a2[0]; // foo bar:foo bar


the = operator means assignment. =& operator means reference.

ecmascript (which is what actionscript/javascript are dervitives of), can be wierd when it comes to references.


var i;
var x;
i = x; // i & x are two seperate variables.

var i;
i = some.global.scope.object; // i is a reference of that object

efficiency

Date: 2006-09-18 01:23 am (UTC)
From: (Anonymous)
efficiency and php on the same line?
riiiiiiight...

b

Profile

metawidget: A platypus looking pensive. (Default)
metawidget
Page generated Jan. 16th, 2026 02:17 pm

June 2025

S M T W T F S
123 4567
89 1011121314
15 161718192021
22 232425 262728
2930     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Powered by Dreamwidth Studios