How to Get Last Key of Associative Array Using PHP

Last Updated on January 8, 2021 by Roshan Parihar

In this tutorial, learn how to get last key of an associative array in PHP. The short answer is: use the PHP end() in combination with the key() that gives you the final key.

You can also use the other methods like PHP foreach loop or for loop to access the elements and find the last key from the elements of an associative array. However, the loop requires to perform all the iteration and find the final key outside of the loop.

The associative array contains the elements including keys and their values. The keys are user assigned keys all are of string type. Let’s find out how to access the elements and find the end key with the examples given below:

Get the Last Key of Associative Array Using end() and key() in PHP

To get the last key from the elements from an associative array, you have to use the PHP end() function. The function sets the internal pointer of the array to the final element. After that, you can use the key() of PHP to get the key of the final element in PHP as given in the example below:

Output

Cars

The above example gives output that is the final key of the array. It requires less coding to use the above functions of PHP and get the end items of an associative array in PHP.

Find Final Key of Array with PHP Foreach Loop

To find the last key from an associative array, you can use the PHP foreach loop that takes a single argument as the associative array variable. It also uses the $key => $value to get the items of the array in PHP. The loop traverses through every element of the array. But, you have to store the key in a variable in each iteration to get the final key outside of the loop in the output as given in the example below:

Output

Cars

The above example gives the required output after the final iteration of the loop. The example is useful to parse through every element and find the end key-value pairs in the output.

Using For Loop to Get the Starting Keys of Associative Array in PHP

The for loop traverses through the elements of an associative array till it reaches the final element. However, you have to count the size of the array using count(), and store the result in a variable for iteration. After that, it also requires to return the keys of an associative array and store them a variable using the PHP array_keys(). But, one thing you should note here that it requires all the iteration to get the last item as given below:

Output

Cars

The above showing the same final keys in the output that you have to find from an associative array variable.

key() with array_reverse() to Find the Final Item Using PHP

In addition to the above all method, you can also use the key() with the array_reverse() to find the final element of an associative array in PHP. See the example given below to get the required result in the output as given below:

Output

Cars

The above example gives you the same output that you get using the other methods given above.

You can use any of the methods given above to find the required final matching item from the given associative array variable in PHP.

You May Also Like to Read

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.