PHP how to get all the keys of an associative array?

I am explaining you, how to get all the keys of an associative array in PHP. we need to use PHP array_keys() function to extract the keys in an associative array.

Example :

<?php
  $websites = array("webdeveloperindia"=>"https://webdeveloperindia.in","pakizacity"=>"https://pakizacity.com","markdevelopments"=>"https://markdevelopments.com","wordpressminds"=>"https://wordpressminds.com");
  print_r(array_keys($websites));
?>

Output :

Array ( [0] => webdeveloperindia [1] => pakizacity [2] => markdevelopments [3] => wordpressminds )

Otherway, I will apply foreach loop so it will also show key and its related values.

Example :

<?php
  $websites = array("webdeveloperindia"=>"https://webdeveloperindia.in","pakizacity"=>"https://pakizacity.com","markdevelopments"=>"https://markdevelopments.com","wordpressminds"=>"https://wordpressminds.com");
  foreach($websites as $key => $website){
    echo $key . " : " . $website . "<br>";
  }
?>

Output :

webdeveloperindia : https://webdeveloperindia.in
pakizacity : https://pakizacity.com
markdevelopments : https://markdevelopments.com
wordpressminds : https://wordpressminds.com

Leave a Reply

Your email address will not be published. Required fields are marked *

13 − = 8