Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

array_key_last

Returns the last key of an array.

Syntax

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

Parameters

ParameterTypeDescription
$arrayarrayThe input array.

Return value

int|string|nullThe last key, or `null` for an empty array.

Examples

The last key and element:

<?php
 
$steps = ['cart' => 1, 'address' => 2, 'payment' => 3];
$last = array_key_last($steps);
 
echo $last, ' ', $steps[$last];

Output:

payment 3

Notes

Unlike end(), the function does not move the array's internal pointer.

See also

  • array_key_first — Returns the first key of an array.
  • end — Returns the last element of an array.
  • array_pop — Removes the last element of an array and returns it.

In the tutorial

  • Array functions — Processing arrays in PHP without loops — array_map, array_filter, array_reduce, searching and slicing.

Thanks for reading!

Donate to the site — we work hard for you.