Page 1 of 1

2 array variables seem to be the same array

Posted: 22 Apr 2023 12:06
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?

Re: 2 array variables seem to be the same array

Posted: 22 Apr 2023 12:47
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.

Re: 2 array variables seem to be the same array

Posted: 23 Apr 2023 12:20
by admin
Arrays are not strange in XY, but this was a bug, thanks for spotting it! :tup: Fixed in next beta. :cup:

Re: 2 array variables seem to be the same array

Posted: 24 Apr 2023 14:23
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.