6

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

 2 years ago
source link: https://www.laravelcode.com/post/how-to-get-all-the-keys-of-an-associative-array-in-php
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

  984 views

  1 year 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:

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

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

<?php
$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]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK