array_key_last

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

Gets the last key of an array without moving the internal pointer.

C C23

C23
#include <stdio.h>

// C has no built-in associative array; a struct array + the last index gives the last key
struct Entry {
    const char *name;
    int age;
};

int main(void) {
    struct Entry ages[] = {{"Peter", 35}, {"Ben", 37}, {"Joe", 43}};
    int n = sizeof ages / sizeof ages[0];
    printf("%s\n", ages[n - 1].name);
    return 0;
}
Output
Joe

C++ C++26

C++26
#include <iostream>
#include <map>
#include <string>

int main() {
    // std::map sorts by key ascending, so rbegin() is the largest key
    std::map<std::string, int> ages{
        {"Peter", 35}, {"Ben", 37}, {"Joe", 43}};
    std::cout << ages.rbegin()->first << "\n";
    return 0;
}
Output
Peter

C# v11.0.100

v11.0.100
using System.Collections.Generic;

// Dictionary is unordered; an ordered mapping is idiomatically a list of key-value pairs, with the ^1 indexer for the last entry
var ages = new List<KeyValuePair<string, int>>
{
    new("Peter", 35), new("Ben", 37), new("Joe", 43),
};
Console.WriteLine(ages[^1].Key);
Output
Joe

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
	// map is unordered, so "the last key" is meaningless; a slice takes the last element in insertion order
	ages := []struct {
		name string
		age  int
	}{{"Peter", 35}, {"Ben", 37}, {"Joe", 43}}
	fmt.Println(ages[len(ages)-1].name)
}
Output
Joe

Java v27.0.1

v27.0.1
import java.util.LinkedHashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        // LinkedHashMap keeps insertion order; convert to a List to get the last key
        Map<String, Integer> ages = new LinkedHashMap<>();
        ages.put("Peter", 35);
        ages.put("Ben", 37);
        ages.put("Joe", 43);
        var keys = new java.util.ArrayList<>(ages.keySet());
        System.out.println(keys.get(keys.size() - 1));
    }
}
Output
Joe

JavaScript v26.13.1

v26.13.1
// Object.keys then at(-1) for the last entry (the ES2022 at)
const ages = { Peter: 35, Ben: 37, Joe: 43 };
console.log(Object.keys(ages).at(-1));
// An empty object yields undefined
console.log(Object.keys({}).at(-1));
Output
Joe
undefined

PHP v8.6.0

v8.6.0
// Available since PHP 7.3: returns the last key without moving the internal pointer
$ages = ["Peter" => 35, "Ben" => 37, "Joe" => 43];
echo array_key_last($ages), "\n";
// An empty array returns null
var_dump(array_key_last([]));
Output
Joe
NULL

Python v3.15.2

v3.15.2
# dicts preserve order: convert to a key list and take the last (or next(reversed(d)))
ages = {"Peter": 35, "Ben": 37, "Joe": 43}
print(list(ages)[-1])
# An empty dict falls back to the default value
print(next(iter({}), None))
Output
Joe
None

Rust v1.99.1

v1.99.1
use std::collections::BTreeMap;

fn main() {
    // HashMap is unordered; BTreeMap iterates by key order for the largest key (1.66+)
    let mut ages = BTreeMap::new();
    ages.insert("Peter", 35);
    ages.insert("Ben", 37);
    ages.insert("Joe", 43);
    println!("{}", ages.last_key_value().unwrap().0);
}
Output
Peter

TypeScript v26.13.1

v26.13.1
// Object.keys then at(-1) for the last entry (the ES2022 at)
const ages: Record<string, number> = { Peter: 35, Ben: 37, Joe: 43 };
console.log(Object.keys(ages).at(-1));
// An empty object yields undefined
console.log(Object.keys({}).at(-1));
Output
Joe
undefined

Language differences

C: struct array with the last index n - 1; the element count is conventionally computed with a sizeof division.

C++: std::map orders by key ascending, so rbegin()->first is the largest key; unordered_map is unordered.

C#: Dictionary is unordered; a key-value list uses the [^1] range indexer for the last item.

Go: map iteration is randomized; a slice takes len - 1 in insertion order.

Java: LinkedHashMap preserves insertion order; convert to ArrayList and take the last item (Java 21's SequencedMap::lastEntry is more direct).

JavaScript: Object.keys($array).at(-1) (ES2022's at supports negative indices).

PHP: PHP arrays are ordered; array_key_last (PHP 7.3) replaces the old end($array) + key($array) dance (moves the internal pointer and warns on empty arrays). It pairs with array_key_first, completing pointer-free boundary-key access.

Python: dict preserves order; list(d)[-1] or next(reversed(d)).

Rust: HashMap is unordered; BTreeMap::last_key_value (1.66+) returns the largest key in key order — semantics are "maximum key", not "last inserted".

TypeScript: same as JavaScript's Object.keys().at(-1).