Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

array_key_last()

Read the last key in the current iteration order without advancing or resetting the internal pointer.

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

Syntax

array_key_last(array $array): int|string|null

Read the last key in the current iteration order without advancing or resetting the internal pointer. Sorting can change which key comes last without reinserting entries. An empty array returns null. Use a strict check when zero is a possible key.

Signature

array_key_last(array $array): int|string|null

Example output

[2,8,null]

Examples

<?php
$history = [8=>'first', 2=>'last'];
echo json_encode([array_key_last($history), key($history), array_key_last([])]);

Result

[2,8,null]

Sources