2 array variables seem to be the same array

Discuss and share scripts and script files...
Post Reply
altoclef
Posts: 30
Joined: 12 Oct 2012 18:54

2 array variables seem to be the same array

Post by altoclef »

Code: Select all

    $array1 = array();
    $secondArray = array();

    echo "array1 has " . count($array1) . " elements<crlf>secondArray has " . count($secondArray) . " elements";

    $array1ElementCount = explode($array1, "element1|element2|element3");

    echo "array1 has " . count($array1) . " elements<crlf>secondArray has " . count($secondArray) . " elements";
The first echo reports that each array has 0 elements, as expected.
However, the second echo reports that each array now has 3 elements. It seems that $array1 and $secondArray are the same array.

I'm expecting to have 2 separate arrays, not 2 variables pointing at the same array. Have I misunderstood what the array() function does, or made another error?

PeterH
Posts: 2776
Joined: 21 Nov 2005 20:39
Location: Germany

Re: 2 array variables seem to be the same array

Post by PeterH »

Sad to say, but you shouldn't expect array variables to work as you'd expect from variables.
(And I think that's bad!)

Especially you can see it on how explode() and implode() work.

If I'm polite, from my point of view I'd call it strange.

Really sad to say so.
W7(x64) SP1 German
( +WXP SP3 )

admin
Site Admin
Posts: 59751
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: 2 array variables seem to be the same array

Post by admin »

Arrays are not strange in XY, but this was a bug, thanks for spotting it! :tup: Fixed in next beta. :cup:

altoclef
Posts: 30
Joined: 12 Oct 2012 18:54

Re: 2 array variables seem to be the same array

Post by altoclef »

admin wrote: 23 Apr 2023 12:20 ... this was a bug, thanks for spotting it! :tup: Fixed in next beta. :cup:
Works as expected in 24.30.0119. Thanks for your prompt fix.

Post Reply