dcsimg
A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: simple array values assignment?

  1. #1
    Senior Member SubwayDesigns's Avatar
    Join Date
    Oct 2002
    Location
    Lausanne, Switzerland
    Posts
    143

    simple array values assignment?

    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:

    PHP Code:
    <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:

    PHP Code:
    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

  2. #2
    Senior Member SubwayDesigns's Avatar
    Join Date
    Oct 2002
    Location
    Lausanne, Switzerland
    Posts
    143
    I actually found some documentation about this, which I'm going to post for future reference. It's a bug that was fixde 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.

    PHP Code:
    $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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center