How to loop through an associative array and get the key in PHP? Last Updated : 07 Jun, 2024 Comments Improve Suggest changes Like Article Like Report 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. here are some common approaches: Table of Content Using for-each loopUsing array_keys() functionUsing array_walk functionUsing for-each loop In this method, traverse the entire associative array using each loop and display the key elements. Example: Program to loop through the associative array and print keys. php <?php // Loop through associative array and get // the key of associative array // Associative array $person_weight = array( "Rajnish" => 58, "Sanjeev" => 55, "Ravi" => 60, "Yash" => 60, "Suraj" => 48 ); // Use for-each loop and display the // key of associative array foreach($person_weight as $key => $value) { echo "Key: " . $key . "\n"; } ?> OutputKey: Rajnish Key: Sanjeev Key: Ravi Key: Yash Key: Suraj Using array_keys() functionThe 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. Syntax: array array_keys( $input_array, $search_value, $strict )Example: Below program illustrate the use of array_keys() function to accessing the keys of associative array. php <?php // Use array_keys() function to display // the key of associative array // Associative array $assoc_array = array( "Geeks" => 30, "for" => 20, "geeks" => 10 ); // Using array_keys() function $key = array_keys($assoc_array); // Calculate the size of array $size = sizeof($key); // Using loop to access keys for( $i = 0; $i < $size; $i++) { echo "key: ${key[$i]}\n"; } ?> Outputkey: Geeks key: for key: geeks Using array_walk functionThe array_walk function applies a user-defined function to every element of the array. This method can be used to access both keys and values of an associative array. Example : Using array_walk function to access the keys of an associative array PHP <?php // Use array_walk() function to display // the key of associative array // Associative array $assoc_array = array( "Alpha" => 1, "Beta" => 2, "Gamma" => 3 ); // User-defined function to display keys function display_key($value, $key) { echo "key: " . $key . "\n"; } // Applying array_walk function array_walk($assoc_array, 'display_key'); ?> Outputkey: Alpha key: Beta key: Gamma Comment More infoAdvertise with us Next Article How to loop through an associative array and get the key in PHP? R Rajnis09 Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads How to Get All Keys of an Associative Array in PHP? Given an Associative array, the task is to get all the keys of the associative array in PHP. There are two approaches to get all keys from the associative array, these are - using array_keys(), and foreach loop. In this article, we will explore these approaches with examples.Table of ContentApproach 2 min read How to remove a key and its value from an associative array in PHP ? Given an associative array containing array elements the task is to remove a key and its value from the associative array. Examples: Input : array( "name" => "Anand", "roll"=> "1") Output : Array ( [roll] => 1 ) Input : array( "1" => "Add", "2" => "Multiply", "3" => "Divide") Outpu 2 min read How to Get All Values from an Associative Array in PHP? Given an Associative Array, the task is to get all values from the associative array in PHP. To get all the values from an associative array, you can use various approaches such as foreach loop, array_values() function, and array_map() function. In this article, we will explore each approach with de 3 min read How to Check if a Key Exists in an Associative Array in PHP? Given an Associative array, the task is to check whether a key exists in the associative array or not. There are two methods to check if a key exists in an associative array, these are - using array_key_exists(), and isset() functions. Let's explore each method with detailed explanations and code ex 3 min read How to check an array is associative or sequential in PHP? In PHP there is no need to write the variable type before the variable because it is loosely-typed. It takes datatype from user defined values that are stored in it. Arrays in PHP is a type of data structure that allows to store multiple elements of similar data type under a single variable thereby 2 min read How to get numeric index of associative array in PHP? In PHP we can associate name/label with each array elements using => symbol. This is very helpful as it is easy to remember the element because each element is represented by the label rather than the index value. Using array_keys() function: The array_keys() function is an inbuilt function in PH 1 min read How to access an associative array by integer index in PHP? There are two types of arrays in PHP, indexed and associative arrays. In case of indexed array strict numeric indexing is followed but in case of associative array there are keys corresponding to each element. The elements of an associative array can only be accessed by the corresponding keys. As th 3 min read How to Check if a Value Exists in an Associative Array in PHP? Given an Associative array, the task is to check whether a value exists in the associative array or not. There are two methods to check if a value exists in an associative array, these are - using in_array(), and array_search() functions. Let's explore each method with detailed explanations and code 2 min read PHP program to add item at the beginning of associative array In PHP associative array is the type of array where the index need not to be strictly sequential like indexed array. Normally add a new element in an existing associative array it will get appended at the end of that array. Example: php <?php // Existing array $arr = array('one' => 1, 'two' = 3 min read How to Add Elements to the End of an Array in PHP? In PHP, arrays are versatile data structures that can hold multiple values. Often, you may need to add elements to the end of an array, whether it's for building a list, appending data, or other purposes. PHP provides several ways to achieve this. In this article, we will explore different approache 3 min read Like