In this tutorial, learn how to get key of first element from an associative array in PHP. The short answer is: use the PHP Show
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 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 PHPTo get the key of the first element from an associative array, you can use the PHP Example PHP1 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 LoopTo 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 Example PHP1 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 PHPThe 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 Example PHP1 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 PHPIn addition to the above all methods, you can also use the PHP 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 keyExample. $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.
|