strange behaviour of each() on references

From: Date: Tue, 27 Nov 2001 08:09:49 +0000
Subject: strange behaviour of each() on references
Groups: php.dev 
Request: Send a blank email to php-dev+get-71744@lists.php.net to get a copy of this message
Hi, I just ran into some strange behaviour, and I was hoping anyone of you could confirm this ... Take a look at the following code snippet ... <? $a = 'a'; $b = 'b'; $c = 'c'; $ar = array( 'a' => &$a, 'b' => &$b, 'c' => &$c ); function p( &$v ) { $v = 'x'; } while( list( $k, $v ) = each( $ar ) ) { p( $v ); print "$k = $v ($a, $b, $c)\n"; } ?> This does not do what I expected ... output: a = x (a, b, c) b = x (a, b, c) c = x (a, b, c) The first version of the example used a foreach() loop to go through the hash key/value pairs. In the manual though I found Note: Also note that foreach operates on a copy of the specified array, not the array itself, therefore the array pointer is not modified as with the each() construct and changes to the array element returned are not reflected in the original array. So this explained why foreach did not return the references stored in the hash. But it made me believe that each() did actually return the keys and values that where in the hash ... so it should return me the original references. The only way I see how to go around this "strange behaviour" is to call the function like this ... p( $ar[$k] ) just ignoring the $v; which I must say just seems wrong :) Can anyone comment on this ? ... Is this the way it should be ? Am I doing something wrong here ? ... Thanks in advance Christophe VG

« previous php.dev (#71744) next »