PHP key() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, key() returns the key of the element that the internal array pointer is pointing at.

Every array has a hidden pointer. current() tells you the value at that pointer and key() tells you the key. Together with next(), prev(), reset() and end() they let you step through an array by hand.

Syntax

Call key() with the values you need. Start with this line.

PHP
$k = key($scores);
CallWhat it does
key($scores)Returns the key at the current pointer position.
key($list)For a normal list the result is a number such as 0, 1 or 2.
key($finished)Returns null when the pointer has moved past the end of the array.

key() does not move the pointer. It only reads the key at the current position. A fresh array always starts with the pointer on the first element.

Library declarations
PHP
key(array|object $array): int|string|null

Parameters

ParameterDescription
arrayRequired. The array whose current key you want.

Return Value

Return ValueDescription
int|string|nullThe key at the pointer position, or null if the pointer is beyond the last element or the array is empty.

PHP Examples

Example 1 — Read the Key at the Pointer

This example reads a key, moves the pointer with next(), and reads the key again.

PHP
<?php
$scores = ["x" => 1, "y" => 2];

echo key($scores), PHP_EOL;

next($scores);
echo key($scores), PHP_EOL;

How It Works

  1. A new array starts with the pointer on its first element, so key() returns "x".
  2. next() moves the pointer forward by one element.
  3. The pointer now points at the second element.
  4. key() returns "y", the key of that element.

Example 2 — key() Returns null Past the End

When the pointer moves beyond the last element there is no current key, so key() returns null.

PHP
<?php
$numbers = [10, 20];

next($numbers);
next($numbers);

var_dump(key($numbers));

How It Works

  1. The array has two elements and the pointer starts on the first.
  2. The first next() moves to the second element.
  3. The second next() moves beyond the last element, so the pointer is no longer valid.
  4. key() has nothing to return, so var_dump() shows NULL.

Example 3 — Keys of a Normal List

For a list created without keys, PHP numbers the elements 0, 1, 2 and so on. key() returns these numbers.

PHP
<?php
$list = ["a", "b", "c"];

echo key($list), PHP_EOL;

end($list);
echo key($list), PHP_EOL;

How It Works

  1. PHP gives the three values the keys 0, 1 and 2.
  2. The pointer starts at the first element, so key() returns 0.
  3. end() moves the pointer to the last element.
  4. key() now returns 2, the key of "c".

Frequently Asked Questions

It returns the key of the element the internal array pointer currently points to. It does not move the pointer.
key() returns the key at the pointer position. current() returns the value at the same position.
The pointer is past the end of the array, or the array is empty. In both cases there is no current element.
No. Only functions such as next(), prev(), reset() and end() change the position.
Usually not. A foreach loop with foreach ($array as $key => $value) is simpler and safer. key() is mainly for manual pointer control.

Next: key_exists()

key_exists() checks whether a given key exists in an array and returns true or false. It i.

key_exists() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful