Content · Programming · PHP

PHP array_key_last() Function: Usage and Examples

Learn how to use PHP array_key_last() to retrieve the last key from indexed and associative arrays, handle empty arrays, and access the final element safely.

2 views Published Oct 16, 2026 Updated Oct 16, 2026 Admin

When working with PHP arrays, you may need to retrieve the last key without knowing its name or numeric index. This is useful for associative arrays, processing queues, and collections with non-sequential keys.

The array_key_last() function provides a simple way to retrieve the last array key without modifying the array.

What Is array_key_last() in PHP?

The array_key_last() function returns the key of the last element in an array.

Its syntax is:

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

The function accepts one array and returns:

  • An integer or string containing the last key.
  • null if the array is empty.

PHP version requirement: array_key_last() was introduced in PHP 7.3 and is available in PHP 8.

The function does not modify the array or change its internal pointer.

Example 1: Getting the Last Key of an Indexed Array

Let's retrieve the last key from a list of programming languages.

<?php

$languages = ["PHP", "Python", "JavaScript"];

$lastKey = array_key_last($languages);

var_dump($lastKey);

Output:

int(2)

The last element is "JavaScript", which has the numeric key 2.

You can use the returned key to retrieve its value:

echo $languages[$lastKey];

Output:

JavaScript

Example 2: Working with Associative Arrays

The function also works with arrays containing named keys.

<?php

$products = [
    "KB-101" => "Keyboard",
    "MS-102" => "Mouse",
    "MN-103" => "Monitor"
];

echo array_key_last($products);

Output:

MN-103

PHP returns the last key according to the array's current element order.

Important: The last key is not necessarily the largest numeric key or the last alphabetically sorted string.

For example:

<?php

$numbers = [
    50 => "First",
    100 => "Second",
    10 => "Third"
];

echo array_key_last($numbers);

Output:

10

The key 10 is returned because it belongs to the last element, even though it is the smallest numeric key.

Handling Empty Arrays

When an array contains no elements, array_key_last() returns null.

<?php

$items = [];

var_dump(array_key_last($items));

Output:

NULL

Before using the returned key to access an element, check that it isn't null:

$lastKey = array_key_last($items);

if ($lastKey !== null) {
    echo $items[$lastKey];
} else {
    echo "The array is empty.";
}

Use a strict comparison rather than if ($lastKey). Integer 0 and an empty string are valid array keys but evaluate to false in a boolean condition.

Practical Example: Processing the Last Imported Record

Imagine an application receives records from an external system. Each record is added to an associative array using its unique ID.

You want to identify the last imported record and display its details.

Create a file named last-record.php:

<?php

declare(strict_types=1);

// Records in import order.

$records = [
    "IMP-201" => [
        "name" => "Keyboard",
        "status" => "Completed"
    ],
    "IMP-202" => [
        "name" => "Mouse",
        "status" => "Completed"
    ],
    "IMP-203" => [
        "name" => "Monitor",
        "status" => "Pending"
    ]
];

// Retrieve the last record ID.

$lastId = array_key_last($records);

if ($lastId !== null) {

    $record = $records[$lastId];

    $message = $lastId
        . " - "
        . $record["name"]
        . " - "
        . $record["status"];

    echo htmlspecialchars(
        $message,
        ENT_QUOTES,
        "UTF-8"
    );

} else {

    echo "No imported records found.";
}

Browser output:

IMP-203 - Monitor - Pending

The function retrieves IMP-203 because it is the last key in the array.

We then use that key to access the corresponding record.

This technique works well when records are maintained in the required order. If you need the record with the latest timestamp, sort or compare timestamps explicitly rather than assuming the last array element is the newest.

array_key_last() vs Related Functions

Function Purpose
array_key_last() Returns the last array key without changing the internal pointer.
array_key_first() Returns the first array key.
array_last() Returns the last array value (PHP 8.5+).
end() Moves the internal pointer to the last element and returns its value.
array_pop() Removes and returns the last array value.

Use array_key_last() when you need the last key without changing the array.

Unlike array_pop(), it does not remove any elements. Unlike end(), it does not change the internal array pointer.

Common Mistakes

  • Expecting the last value: The function returns a key, not its associated value.
  • Assuming numeric keys are sequential: The last key is not always equal to count($array) - 1.
  • Ignoring empty arrays: Check for null before accessing the returned key.
  • Assuming keys are sorted: The function follows the array's current element order.

Key Takeaways

  • array_key_last() was introduced in PHP 7.3.
  • It returns the last key of an indexed or associative array.
  • The result can be an integer, string, or null.
  • An empty array returns null.
  • The function preserves the array and its internal pointer.
  • Use !== null to check the returned key safely.

The array_key_last() function offers a simple, reliable way to access the final key in a PHP array. It is especially useful for associative collections and arrays whose numeric indexes are not sequential.

For additional details, see the official PHP array_key_last() documentation.

Share this resourceSend this page to someone who may find it useful.