How to Get Key of First Element in Associative Array in PHP

Last Updated on January 7, 2021 by Roshan Parihar

In this tutorial, learn how to get key of first element from an associative array in PHP. The short answer is: use the PHP reset() in combination with the key() that gives you the initial key.

You can also use the PHP foreach loop or for loop to access the elements and find the key of the first element of an associative array. However, it requires to use the break statement to stop the loop after the first iteration.

The associative array contains the elements with user assigned keys that are of string type. Let’s find out how to access the elements and find the required starting key with the examples given below:

Get Key of First Element Using reset() and key() in PHP

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

Output

Cycles

The above example prints the first key in the output. This is a simple example that requires less coding to write and get the start items of an associative array in PHP.

Find First Key of Associative Array with PHP Foreach Loop

To find the first key from an associative array, you can use the PHP foreach loop. The loop takes an argument as the associative array variable and the $key => $value to get the items of the array in PHP. It traverses through all the elements of the array. But, you have to use the break statement to stop the loop after the first iteration and print the first key in the output as given in the example below:

Output

Cycles

The above example prints the key in the output after the first iteration. The example is useful to parse the elements and find the starting key-value pairs in the output.

Using For Loop to Get the Starting Keys in PHP

The loop is useful to traverses through all the elements of an associative array. However, it requires counting the size of an associative array and store in a variable for iteration. After that, you also need to return the keys of an array in a variable using the PHP array_keys(). But, one thing you have to note that it requires a single iteration to get the first item that can achieve by using break statement as given below:

Output

Cycles

The above showing the same first keys that you have to find from the given associative array variable.

Get Key of First Element With Array_keys() in PHP

In addition to the above all methods, you can also use the PHP array_keys() to get the starting item as given below:

Output

Cycles

It is the same output that you get above in each method.

All the methods are useful to find the required matching item from an associative array 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.