SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 26
  1. #1
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Copy array (not a reference!!!)

    This is a pretty simple question, but it's driving me nuts.

    I have an array called $results and it has the following values:

    Array
    (
    [0] => Array
    (
    [0] => Kentucky
    [1] => Ontario
    [2] => Lesbon
    [3] => Ottawa
    [4] => Los Angeles
    )

    [1] => Array
    (
    [0] => Liz Claiborne
    [1] => NCP PRECISION MACHINING LTD
    [2] => Haunted Walk of Kingston
    [3] => Diane Stone Insurance Agency Inc
    [4] => City Of Kingston
    )

    [comp] => Array
    (
    [0] => Liz Claiborne
    [1] => NCP PRECISION MACHINING LTD
    [2] => Haunted Walk of Kingston
    [3] => Diane Stone Insurance Agency Inc
    [4] => City Of Kingston
    )

    )


    I would like to copy the value of $results["comp"] into a new
    array called $company. I've tried $company = $results["comp"]; to
    copy all the contents of the array value "comp" into the new
    array, but it seems to be passing it through as a reference. Whenever
    I change $results, so does $comp.

    What the heck am I doing wrong? *embarassed*
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  2. #2
    SitePoint Evangelist ClickHeRe's Avatar
    Join Date
    Mar 2005
    Location
    Ottawa, Canada
    Posts
    580
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you are in PHP 5, there is a clone() function that enables you to make a copy of the object as PHP5 is all reference oriented for objects

    In PHP4 you shouldn't have a problem

  3. #3
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doh. Forgot about that -_-

    Thanks!
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  4. #4
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    err but this isn't an object, it's an array in an array...and I get this msg "Warning: __clone method called on non-object in".
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  5. #5
    SitePoint Evangelist ClickHeRe's Avatar
    Join Date
    Mar 2005
    Location
    Ottawa, Canada
    Posts
    580
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    according to the manual in the Array section
    You should be aware that array assignment always involves value copying.
    You need to use the reference operator to copy an array by reference.
    PHP Code:
    <?php
    $arr1 
    = array(23);
    $arr2 $arr1;
    $arr2[] = 4// $arr2 is changed,
                 // $arr1 is still array(2, 3)
                 
    $arr3 = &$arr1;
    $arr3[] = 4// now $arr1 and $arr3 are the same
    ?>

  6. #6
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    PHP should create a copy of the array (not a reference to it) when using the assignment operator...

    Edit:

    ... and once again I'm typing very, very slowly.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  7. #7
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So in the instance above all I need to use is

    $company = &$results["comp"];
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  8. #8
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    That will create a reference to the array, not a copy. That isn't what you want, right?
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  9. #9
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol...noo. Maybe I'm not explaining myself correctly...

    Say I have:

    $names[] = array("David","Paul","Mike","Conny");
    $names[] = array("Rocco","Gianni","Billy");

    Thus my array looks like this:

    Array
    (
    [0] => Array
    (
    [0] => David
    [1] => Paul
    [2] => Mike
    [3] => Conny
    )

    [1] => Array
    (
    [0] => Rocco
    [1] => Gianni
    [2] => Billy
    )



    )
    Say I want to take a copy of $names[0] which has a sub-array value of "David","Paul","Mike","Conny" and put it into a new array called $new_names. How would I do it? WITHOUT A REFERENCE. So if I edit the new_names array, it wont effect $names.
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  10. #10
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And yes, I am using PHP5.
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  11. #11
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    This should work:

    PHP Code:
    $new_names $names[0]; 
    If it doesn't, something is very wrong.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  12. #12
    SitePoint Evangelist ClickHeRe's Avatar
    Join Date
    Mar 2005
    Location
    Ottawa, Canada
    Posts
    580
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $new_names = $names[0]; <-- according to the help file, this should be the good behavior and if you modify $names[0][0] = 'Roger'; it should not affect $new_names.

    If you don't have that, then something is wrong with PHP =]

    EDIT: well seems i'm the slow one this time :P

  13. #13
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol...

    omg, headaches gallore. The values are changing!!!!
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  14. #14
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Perhaps you can show us how your real code looks?
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  15. #15
    SitePoint Zealot
    Join Date
    May 2004
    Location
    Malaysia
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like array are being referenced by default.

    Would like to know what went wrong...

  16. #16
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you SO much for your help.

    http://www.spyral-productions.com/dd....class.php.txt

    The class is a site crawler using cURL. After every preg_match_all it performs it reassigns the value to $results then dumps the data into $main[]. I'm doing this because the preg match has extra data that eats up memory (in $results).

    So I'm trying to organize the data in one variable for easy reference.

    Sorry for the bad spacing and such, was in a hurry

    BTW: Based on this code, PHP is sending back a CGI Application error...
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  17. #17
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Snecx
    Looks like array are being referenced by default.

    Would like to know what went wrong...
    That's what I am thinking. I haven't tampered with the PHP settings, so anything that was installed was done from the beginning. Most likely I'm probably doing something very wrong
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  18. #18
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    It seems like the link is incorrect. I get a regular page instead of a text file.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  19. #19
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  20. #20
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    It doesn't seem like it's your code that's causing this. To be honest, I don't know what's going on...
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  21. #21
    SitePoint Guru davedibiase's Avatar
    Join Date
    Aug 2001
    Location
    Toronto, Canada
    Posts
    829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Arg. -_- what the hell could it be???? -_-
    ||Dave Di Biase||
    ----------------------------------
    "There are 2 secrets in life. 1) Never say everything you know."
    GFXWARS - The ultimate graphics battle!

  22. #22
    SitePoint Evangelist ClickHeRe's Avatar
    Join Date
    Mar 2005
    Location
    Ottawa, Canada
    Posts
    580
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A bug in PHP ?

    I don't know either what's wrong.

    Perhaps you could post your code and problem to the php.net bug section so someone can hunt it down

  23. #23
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lilleman
    Edit:

    ... and once again I'm typing very, very slowly.
    It's about the Hi,.

  24. #24
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Quote Originally Posted by Young Twig
    It's about the Hi,.
    Nah, I don't think so. I can type that part really fast.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  25. #25
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    AU
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I could be way off, but in the documentation:

    Description
    mixed preg_match ( string pattern, string subject [, array &matches [, int flags [, int offset]]] )

    Notice the "&" in "array &matches" does this imply that it is passed as reference ?


Bookmarks

Posting Permissions

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