How to Get All Keys of an Associative Array in PHP? Last Updated : 16 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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 1: Using the array_keys() FunctionApproach 2: Using a foreach LoopApproach 3: Using the array_walk() FunctionUsing the array_keys() FunctionThe array_keys() function returns an array containing the keys of an associative array. PHP <?php $subjects = array( "Maths" => 95, "Physics" => 90, "Chemistry" => 96, "English" => 93, "Computer" => 98 ); $keys = array_keys($subjects); print_r($keys); ?> OutputArray ( [0] => Maths [1] => Physics [2] => Chemistry [3] => English [4] => Computer ) Using a foreach LoopYou can use a foreach loop to iterate over the associative array and extract the keys of array. PHP <?php $subjects = array( "Maths" => 95, "Physics" => 90, "Chemistry" => 96, "English" => 93, "Computer" => 98 ); $keys = []; foreach ($subjects as $key => $value) { $keys[] = $key; } print_r($keys); ?> OutputArray ( [0] => Maths [1] => Physics [2] => Chemistry [3] => English [4] => Computer ) Using the array_walk() FunctionThe array_walk() function can be used to apply a callback function to each element of the array. We can use this to collect all the keys. PHP <?php $subjects = array( "Maths" => 95, "Physics" => 90, "Chemistry" => 96, "English" => 93, "Computer" => 98 ); $keys = []; array_walk($subjects, function($value, $key) use (&$keys) { $keys[] = $key; }); print_r($keys); ?> OutputArray ( [0] => Maths [1] => Physics [2] => Chemistry [3] => English [4] => Computer )Using array_map() FunctionThe array_map() function can be used to apply a callback function to the elements of an array. We can use this function to extract all the keys from an associative array by mapping the keys to a new array.Example PHP <?php function getArrayKeysUsingMap($array) { return array_map(function($key) { return $key; }, array_keys($array)); } $associativeArray = [ "name" => "John", "age" => 30, "city" => "New York" ]; $keys = getArrayKeysUsingMap($associativeArray); echo "Keys of the associative array are:\n"; print_r($keys); ?> OutputKeys of the associative array are: Array ( [0] => name [1] => age [2] => city ) Comment More infoAdvertise with us Next Article How to Get All Keys of an Associative Array in PHP? B blalverma92 Follow Improve Article Tags : PHP PHP-array Similar Reads Sort an Associative Array by Key in PHP Given an Associative Array, the task is to sort the associative array by its keys in PHP. There are different methods to sort Associative Array by keys, these are described below: Table of ContentUsing ksort() FunctionUsing uksort() FunctionConverting to a Regular Array for SortingUsing array_multis 3 min read How to Insert a New Element in an Array in PHP ? In PHP, an array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index or key.We can insert an element or item in an array using the below function 5 min read Associative Arrays in PHP An associative array in PHP is a special array where each item has a name or label instead of just a number. Usually, arrays use numbers to find things. For example, the first item is at position 0, the second is 1, and so on. But in an associative array, we use words or names to find things. These 4 min read How to create an array with key value pairs in PHP? In PHP, an array with key-value pairs is called an associative array. It maps specific keys to values, allowing you to access elements using custom keys rather than numerical indices. Keys are strings or integers, while values can be of any data type.Here we have some common approaches to create an 2 min read How to delete an Element From an Array in PHP ? To delete an element from an array means to remove a specific value or item from the array, shifting subsequent elements to the left to fill the gap. This operation adjusts the array's length accordingly, eliminating the specified element.This article discusses some of the most common methods used i 4 min read Like