PHP array_key_last() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_key_last() returns the last key of an array without changing the array.

It returns null for an empty array. This replaces the older trick of calling end() and key().

Syntax

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

PHP
$lastKey = array_key_last($items);
CallWhat it does
array_key_last(["x" => 1, "y" => 2])Returns "y".
array_key_last(["a", "b", "c"])Returns 2, the index of the last item.
array_key_last([])Returns null for an empty array.

The result is the last key in the array's order, not the largest key.

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

Parameters

ParameterDescription
arrayRequired. The array whose last key you want.

Return Value

Return ValueDescription
int|string|nullThe last key of the array, or null if the array is empty.

PHP Examples

Example 1 — Get the Last Key

Read the last key of an associative array.

PHP
<?php
$scores = ["ana" => 90, "bob" => 80, "cara" => 70];

echo array_key_last($scores), PHP_EOL;

How It Works

  1. Create an associative array. The last key added is cara.
  2. Call array_key_last(). It looks at the end of the array.
  3. Print the result. The output is the key, not the value 70.
  4. The array stays the same. No pointer is moved.

Example 2 — Empty Arrays and Unordered Keys

See what happens with an empty array or keys that are not sorted.

PHP
<?php
echo var_export(array_key_last([]), true), PHP_EOL;
echo array_key_last([3 => "a", 1 => "b"]), PHP_EOL;
echo gettype(array_key_last(["a", "b"])), PHP_EOL;

How It Works

  1. Use an empty array. There is no last key, so PHP returns null.
  2. Print null safely. var_export() shows NULL.
  3. Use keys 3 then 1. The last key is 1 because it was added last.
  4. Check the type. A numeric list gives an integer key.

Example 3 — Read the Last Item and Add a New One

Use the last key to read the final value, then see how a new item changes it.

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

$last = array_key_last($list);
echo $last . " => " . $list[$last] . PHP_EOL;

$list[] = "d";
echo array_key_last($list), PHP_EOL;

How It Works

  1. Create a list. It has three items with keys 0, 1 and 2.
  2. Get the last key. It is 2, and the value is c.
  3. Add a new item. $list[] = "d" gets the next key, 3.
  4. Ask again. The last key is now 3.

Frequently Asked Questions

It returns the last key of an array, or null if the array is empty. It does not change the array.
It was added in PHP 7.3. For older versions use end() followed by key().
Use $array[array_key_last($array)] after checking the array is not empty, or array_last() in PHP 8.5.
No. It is the key at the end of the array order. [3 => "a", 1 => "b"] gives 1.
array_key_first() returns the first key of an array.

Next: array_keys()

array_keys() returns all the keys of an array, or only the keys whose value matches a filt.

array_keys() 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