Current Location: Home> Latest Articles> end() + key(): Get the key name of the last element

end() + key(): Get the key name of the last element

M66 2025-06-02

In PHP, array operations are very common. Getting the last element of an array is usually a simple operation, but how do you get the key name of the last element at the same time? At this time, the two functions end() and key() can help us achieve this goal.

This article will explain how to use these two functions to get the key name of the last element of the array and provide some practical examples to demonstrate their usage.

What are end() and key() ?

  • end() : This function points the inner pointer of the array to the last element and returns the value of that element.

  • key() : This function returns the key name of the current array element, which is usually used to obtain the key name of the element pointed to by the current pointer position of the array.

Use end() and key() to get the key name of the last element

To get the key name of the last element of the array, you can first use end() to point the pointer of the array to the last element. Then, use key() to get the key name of the element pointed to by the current pointer.

Here is a sample code:

 <?php
// Example array
$array = [
    'first' => 'apple',
    'second' => 'banana',
    'third' => 'cherry'
];

// useend()Point the array pointer to the last element
end($array);

// usekey()Get the key name of the last element
$last_key = key($array);

echo "The key name of the last element is: " . $last_key;  // Output: The key name of the last element is: third
?>

Code parsing

  1. Defining an array : We first create an associative array called $array , which contains three elements, including first , second and third as keys.

  2. Calling end() : By calling end($array) , we move the inner pointer of the array to the last element of the array. At this pointer points to 'third' => 'cherry' .

  3. Call key() : Then, using the key($array) function, we get the key name of the element pointed to by the current pointer. In this example, the returned key name is 'third' .

Things to note

  • If the array is empty, end() will set the pointer to false , and calling key() at this time will return false .

  • The end() function not only gets the last element, but also affects the internal pointer of the array, which means that if other array operation functions are called later, the pointer will start from the last element.

Application in actual development

In actual development, sometimes we need to do some processing based on the last element of the array. For example, when you get data from the API and process the results, you may need to get information about the last item of data. This is very convenient to use end() and key() .

Example: Get the last piece of data returned by the API

Suppose we get some data from some API interface and want to get the identifier of the last record. We can use codes like the following:

 <?php
// Assume this is fromAPIReturned results
$response = [
    'data' => [
        'item1' => ['id' => 1, 'name' => 'Item 1'],
        'item2' => ['id' => 2, 'name' => 'Item 2'],
        'item3' => ['id' => 3, 'name' => 'Item 3']
    ]
];

// Get the key name of the last data
end($response['data']);
$last_key = key($response['data']);

echo "The key name of the last data is: " . $last_key;  // Output: The key name of the last data is: item3
?>

Summarize

end() and key() are very useful array functions in PHP. Through them, we can easily get the key name of the last element of the array. In actual development, understanding how these functions can make you more efficient when processing array data.