How to get the last key in an array with PHP?

Spread the love

Sometimes, we want to get the last key in an array with PHP.

In this article, we’ll look at how to get the last key in an array with PHP.

How to get the last key in an array with PHP?

To get the last key in an array with PHP, we can use the array_key_last function.

For instance, we write

$array = ['apple' => 10, 'grape' => 15, 'orange' => 20];
echo array_key_last($array)

to call array_key_last with the $array associative array to return the key of the last entry in $array.

Conclusion

To get the last key in an array with PHP, we can use the array_key_last function.

Leave a Reply

Your email address will not be published.