PHP key

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP: key() function

PHP: Fetch a key from an array

The key() function is used to get the index element of the current array position.

Version

(PHP 4 and above)

Syntax

key(array_name) 

Parameter

Name

Description

Required /
Optional

Type

array_name

Array to use.

Required

Array

Return value

The key of the current array element

Value Type : Mixed*.

*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Example :

  1. $subjects = array(Subject1 => "Physics", Subject2 => "Chemistry", Subject3 => "Biology");  

  2. echo "Current position Key is : " . key($subjects);  

  3. ?>   

Output :

Current position Key is : Subject1