Home > Article > Backend Development > How do I Get the Last Key of an Array in PHP?
Retrieving the Last Array Key
This question revolves around obtaining the last key in an array.
Solution:
A practical approach involves the combined usage of PHP's end() and key() functions.
Subsequently, the following code exemplifies this:
<code class="php">$array = array( 'first' => 123, 'second' => 456, 'last' => 789, ); end($array); $key = key($array); var_dump($key);</code>
This code will output:
<code class="php">string 'last' (length=4)</code>
indicating the last key in the given array.
It's important to note that the internal pointer will remain at the end of the array after this operation. Consider using reset() to return the pointer to the beginning if desired.
The above is the detailed content of How do I Get the Last Key of an Array in PHP?. For more information, please follow other related articles on the PHP Chinese website!