Associative Array: Associative arrays are used to store key-value pairs. For example, to store the marks of the different subject of a student in an array, a numerically indexed array would not be the best choice. Instead, we could use the respective subject’s names as the keys in our associative array, and the value would be their respective marks gained. In associative array, the key-value pairs are associated with => symbol.
Method 1: In this method, traverse the entire associative array using foreach loop and display the key elements.
Program: Program to loop through associative array and print keys.
Key: Rajnish Key: Sanjeev Key: Ravi Key: Yash Key: Suraj
Method 2: Using array_keys() function: The array_keys() is an inbuilt function in PHP which is used to return either all the keys of array or the subset of the keys.
array array_keys( $input_array, $search_value, $strict )
Program: Below program illustrate the use of array_keys() function to accessing the keys of associative array.
key: Geeks key: for key: geeks
- How to remove a key and its value from an associative array in PHP ?
- Iterate associative array using foreach loop in PHP
- How to get numeric index of associative array in PHP?
- Convert an object to associative array in PHP
- How to check an array is associative or sequential in PHP?
- How to access an associative array by integer index in PHP?
- PHP program to add item at the beginning of associative array
- Multidimensional Associative Array in PHP
- Associative Arrays in PHP
- Difference between Primary key and Super key
- PHP | Separate odd and even elements from array without using loop
- C# | How to get hash code for the specified key of a Hashtable
- Determine the first and last iteration in a foreach loop in PHP?
- How to download a CSV file in PHP that is triggered through a URL ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.