Arrays as Parameters to user functions in array_walk -- PHP4

From: Date: Wed, 11 Oct 2000 00:46:43 +0000
Subject: Arrays as Parameters to user functions in array_walk -- PHP4
Groups: php.general 
Request: Send a blank email to php-general+get-19451@lists.php.net to get a copy of this message
There appears to be an issue when passing an array as the 3rd parameter to array_walk. Consider the following code, which passes a simple variable to array_walk <? function test ($item, $key, &$aString ) { if ($item == ':') { $aString = $key; echo "aString is now " . $aString . "<BR>"; } } $firstArray = array (",", "/", ":", "-", "*"); $aString = "x"; echo "aString equals $aString<BR>"; array_walk ($firstArray, test, &$aString); echo "After array_walk<BR>"; echo "aString equals $aString<BR>"; ?> This outputs aString equals x aString is now 2 After array_walk aString equals 2 as expected. However, if the 3rd parameter is an array, the results are not as expected function test ($item, $key, &$anotherArray ) { if ($item == ':') { $anotherArray[0] = $key; echo "anotherArray[0] is now " . $anotherArray[0] . "<BR>"; } } $firstArray = array (",", "/", ":", "-", "*"); $anotherArray = array ("x", "y", "z"); while (list ($key, $val) = each ($anotherArray)) echo "anotherArray element $key equals $val<BR>"; array_walk ($firstArray, test, &$anotherArray); echo "After array_walk<BR>"; reset ($anotherArray); while (list ($key, $val) = each ($anotherArray)) echo "anotherArray element $key equals $val<BR>"; ?> outputs: anotherArray element 0 equals x anotherArray element 1 equals y anotherArray element 2 equals z anotherArray[0] is now 2 After array_walk anotherArray element 0 equals 4 anotherArray element 1 equals y anotherArray element 2 equals z anotherArray's 0th element is 4, when it should be 2

« previous php.general (#19451) next »