How to get all the keys of an associative array in PHP

  1346 views   2 years ago PHP

Use the PHP array_keys() function

You can use the PHP array_keys() function to get all the keys out of an associative array.

Let's try out an example to understand how this function works:

$cities = array("France"=>"Paris", "India"=>"Mumbai", "UK"=>"London", "USA"=>"New York");
// Get keys from cities array

You can also use the PHP foreach loop to find or display all the keys, like this:

$cities = array("France"=>"Paris", "India"=>"Mumbai", "UK"=>"London", "USA"=>"New York");
// Loop through cities array
foreach($cities as $key => $value){
    echo $key . " : " . $value . "<br>";


Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]