How to get all the keys of an associative array in PHP
array_keys()
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>"; } ?>
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Use the PHP
array_keys()functionYou 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:
You can also use the PHP foreach loop to find or display all the keys, like this:
need an explanation for this answer? contact us directly to get an explanation for this answer