How to get the key of array in PHP?

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:

Table of Contents

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:

Example

PHP

1

2

3

$myarrayassoc = array("Cycles" => 3, "Bikes" => 6, "Cars" => 11);

reset($myarrayassoc);

echo key($myarrayassoc);

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:

Example

PHP

1

2

3

4

5

6

$myarrayassoc = array("Cycles" => 3, "Bikes" => 6, "Cars" => 11);

//traversing elements of an associative array

foreach ($myarrayassoc as $key => $val){

echo "Key of first element is: ".$key;

    break;

}

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:

Example

PHP

1

2

3

4

5

6

7

8

9

10

$myarrayassoc = array("Cycles" => 4, "Bikes" => 9, "Cars" => 13);

//Get size of array elements

$arrsize = count($myarrayassoc);

//Return array keys in a variable

$keys = array_keys($myarrayassoc);

//Iterate elements of an associative array

for ($x = 0; $x < $arrsize; $x++){

echo "Key of first element is: ".$keys[$x];

    break;

}

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:

How do I get the key of an array element?

The key() function simply returns the key of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, key() returns null .

How to print array key

Example. $a=array(10,20,30,"10"); print_r(array_keys($a,"10",true));

How to get object keys in PHP?

To display only the keys from an object, use array_keys() in PHP.

What is array_keys () used for?

The array_keys() is a built-in function in PHP and is used to return either all the keys of and array or the subset of the keys. Parameters: The function takes three parameters out of which one is mandatory and other two are optional.