PDA

View Full Version : simple array values assignment


Subwaydesigns
07-13-2007, 06:24 PM
Hello everybody,

There must be a very obvious answer to this. I am not especially well-versed in server side scripting, although I usually manage to get by... but this time I'm stuck.

Let me explain what I'm trying to do. I have an xml file for a gallery I am displaying in flash. I'm trying to build a CMS for this gallery - nothing much, just an upload/order/delete images utility.

Now, I use an XML parser class in php, that creates an object containing my xml nodes (with tags, subnodes, attributes etc.) as arrays. For example, say I have an xml file that looks like:


<images>
<pic>
<image>imageinfo</image>
<thumbn>thumbinfo</thumbn>
<caption>captioninfo</caption>
</pic>
</images>


I can access thumbinfo using $parser->document->pic[0]->thumbn[0]->tagData.


Ok, enough background. What I'm trying to do is very simple - I just want to add an element to the $parser->document->pic array (which will add a new node when the xml is generated).

Here's the code I'm using:


function newim() {
global $parser;

$parser->document->pic[0]->thumbn[0]->tagData = "original";

$parser->document->pic[3] = $parser->document->pic[0]; //the original pic array has 3 elements (pic[0], pic[1], pic[2]); I am adding a fourth.

$parser->document->pic[3]->thumbn[0]->tagData = "new";

echo $parser->document->pic[0]->thumbn[0]->tagData; //output: new

echo $parser->document->pic[3]->thumbn[0]->tagData; //output: new
}


can anyone explain this behavior? I would think that the value $parser->document->pic[0]->thumbn[0]->tagData never got changed - that is, I would expect this code to output "original" and then "new". I suspect it might have something to do with variable scope, but I've been fighting with this code all morning and I'm not getting anywhere.

Any ideas or insight you can provide would be greatly appreciated.

Marc

LOLFlash
07-14-2007, 02:36 PM
I dont know PHP but in flash logically all right:

string "original" it is your data
$parser->document->pic[0]->thumbn[0]->tagData it is pointer to your data: it tells computer where to find string "original"
after:
$parser->document->pic[3] = $parser->document->pic[0];

you have two pointers to the same data:
$parser->document->pic[3]->
and
$parser->document->pic[0]->

both point to same Object : thumbn[0]->tagData="original"



if computer use ether of this pointers:
$parser->document->pic[3]->thumbn[0]->tagData
or
$parser->document->pic[0]->thumbn[0]->tagData
it bring him to string "original"

does it help?

Subwaydesigns
07-14-2007, 04:33 PM
Thankd for the reply LOLFlash, it does help. I actually found some documentation about this - it's a bug that was fixed in later versions of php (I'm stuck with php 4.4 on this project).

For anyone who is interested, here is the link:
http://bugs.php.net/bug.php?id=12775

And the workaround is actually simple; you can use the following to make a 'deep copy' of arrays/objects instead of just giving a new pointer to some data.


$parser->document->pic[3] = unserialize(serialize($parser->document->pic[0]));


This will act as I had expected, creating a full copy of $parser->document->pic[0] (preserving references etc.) at $parser->document->pic[3], and not simply adding a pointer to the same data.