get key of array (key function sucks?)

From: Date: Fri, 27 Jan 2006 18:49:22 +0000
Subject: get key of array (key function sucks?)
Groups: php.general 
Request: Send a blank email to php-general+get-229374@lists.php.net to get a copy of this message
Hallo, I am adding a value to a session variable (an array). Then I want to place the key of the array in another session variable to keep it for later use (so I can return to the array to write data to it). The key function should work there, but it seems to be a bit strange. Imagine: $array = array(); $array[] = "Yellow"; $array[] = "Geen"; $array[] = "Red"; foreach ($array as $key=>$value) { print ("Key: " . $key . " has color " . $value . " | "); print ("current key = " . key($array) . "<br/>"); } If you run the loop the key value returned from key is one higher than the key in $key, and the last key is null? How do I get the present key without a problem?? Thanks!!

« previous php.general (#229374) next »