Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

key()

Read the current array key without moving the internal pointer.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

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

Read the current array key without moving the internal pointer. A key can be zero or the empty string; only null means that there is no current element. Passing an object is deprecated since PHP 8.1.

Signature

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

Example output

[0,"",null]

Examples

<?php
$items = [0=>"first",""=>"second"];
$keys = [key($items)]; next($items); $keys[] = key($items);
next($items); $keys[] = key($items);
echo json_encode($keys);

Result

[0,"",null]

Sources