PHP key() Function

Example

Returns the key name of the element from the current internal pointer position:

<?php
$people = array("Bill", "Steve", "Mark", "David");
echo "The current position of the key is: " . key($people);
?>

Running Instance

Definition and Usage

The key() function returns the key name of the element currently pointed to by the internal pointer of the array.

If an error occurs, the function returns FALSE.

This function is similar to current() Similar, but the returned results are different. The current() function returns the value of the element, while the key() function returns the key name of the element.

Syntax

key(array)
Parameter Description
array Required. Specifies the array to be used.

Technical Details

Return Value: Returns the key name of the array element pointed to by the current internal pointer.
PHP Version: 4+