David, thank you very much. I suspected smth. like this, but still, it is
weird: PHP already has the "&" operator (?) for assigning by reference. And
there is more: I noticed that if I use the "sort" function instead of
"array_multisort", it works as expected.

I still think there is a bug involved :(

>>I tried something like this:
>>
>><?
>>$arrayA = array(5,4,3,2,1);
>>$arrayB = $arrayA;
>>array_multisort($arrayA);
>>echo $arrayA[0], "<br>";
>>echo $arrayB[0];
>>?>
>>
>>
>>The output is:
>>1
>>1
>>
>>I think it should be:
>>1
>>5

DO> It's not a bug (ie this is expected behaviour in PHP4 for various
DO> sensible reasons), but it can sometimes throw up odd effects if you're
DO> not expecting it.

DO> What you're running into here is the difference between a deep copy
DO> (make a copy of a piece of memory) and a shallow copy (make two
DO> variables point to the same piece of memory).

DO> The way to think of it is that $arrayA doesn't actually contain your
DO> array values - it contains a reference (a pointer in C-speak) to the
DO> memory location where the array values are stored. The line

DO> $arrayB = $arrayA;

DO> is copying the reference, not the values. You can run up against this
DO> behaviour in quite a few of the post-C++ languages, and it can be
DO> disconcerting if you're used to languages where copies are all deep
DO> unless flagged otherwise.

DO> To add insult to injury, some array operations can implicitly cause a
DO> deep copy to be made. Try this, which adds one extra line:

DO> <?
DO> function echo_array($a) {
DO>         foreach ($a as $v) {
DO>                 echo ($v);
DO>                 echo (', ');
DO>         }
DO>         echo ("<br>");
DO> }
DO> $arrayA = array(5,4,3,2,1);
DO> $arrayB = $arrayA;
DO> $arrayA[] = 6; // this line added
DO> array_multisort($arrayA);
DO> echo_array($arrayA);
DO> echo_array($arrayB);
?>>

DO> More details (maybe) here:

DO> http://www.zend.com/zend/art/ref-count.php




-- 
 Ciprian

> Un cuvant de sfarsit:
> A cynic knows the price of everything & value of nothing


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to