note 37680 deleted from control-structures.foreach by sniper

From: Date: Sat, 29 Nov 2003 03:22:20 +0000
Subject: note 37680 deleted from control-structures.foreach by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-61104@lists.php.net to get a copy of this message
Note Submitter: chernyshevsky@hotmail.com ---- Note that foreach() will not return the key correctly if it contains a terminal zero ("\x00"). The key would get truncated. Consider the following: $test = array("A\x00B" => "Hello world"); foreach($test as $key => $value) { echo "$key = $value<br>"; } echo '$test["A"] = ' . $test["A"] . "<br>"; echo '$test["A\x00B"] = ' . $test["A\x00B"] . "<br>"; The output in PHP 4.3.2 is: A = Hello world $test_array["A"] = $test_array["A\x00B"] = Hello world The while(list(...) = each(...)) construction would yield the same result. In order to retrieve the correct key, you need to use array_keys(): $keys = array_keys($test); foreach($keys as $key) { $value = $test[$key]; echo "$key = $value<br>"; }

« previous php.notes (#61104) next »