key

mixed key(array array) 

Returns the key for the element in the current position.

Returns:

Key of the current array element; FALSE on error

Description:

key() is used to retrieve the key of the element to which the internal array pointer is currently set.

Version:

PHP 3, PHP 4

See also:

current() 
end() 
next() 
prev() 
reset() 

Example:

Fetch the key of the current array element
$my_array = array("x" => "two", "z" => "four", "y" => "three", "w" => "one"); 
do {
   echo "Key: ", key($my_array), "\n"; 
} while (each($my_array)); 

Output: 
Key: x 
Key: z 
Key: y 
Key: w 

Get PHP Functions Essential Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.