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:
1 2 3 |
$myarrayassoc = array("Cycles" => 3, "Bikes" => 6, "Cars" => 11); reset($myarrayassoc); echo key($myarrayassoc); |
Output
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:
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
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:
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
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:
1 2 |
$myarrayassoc = array("Cycles" => 4, "Bikes" => 9, "Cars" => 13); echo array_keys($myarrayassoc)[0]; |
Output
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