PHP key array function

Last Updated: February 20, 2022

key – Fetch key from the array. Key of the current index position

Syntax

key(array|object $array): int|string|null

Supports (PHP 4 , PHP 5, PHP 7, PHP 8)

Parameters

Parameter Description
array target array

Return Value

The key() function simply returns the key of the array element that’s currently being pointed to by the internal pointer.

Example

$my_array = array("a" => "One","b" => "Two", "c" => "Three");
next($my_array);
echo key($my_array);

Output

b

Test