The function array_keys() is a predefined function of PHP. This array_keys() gives us the functionality of accessing the keys of an associative array by indexing numbers.
Print, key name of an associative array Print all key names by loop Print all keys by indexing number of an assoc. array Print all keys and their values by indexing number, using a loop |
To make the associative array keys, accessible by indexing number, pass that associative array in the argument (parameter) of the array_keys() method.
$array = array_keys($assoc_array);
The function keeps associative array's keys by indexing numbers. So that we can get a particular key name at an indexing number by just passing the indexing number.
$array = array_keys($assoc_array);
echo $array[0];
Once we get the key name by indexing number, It is easiest to get/print value of it.
Print a key name of an associative array
<?php
$assoc_array = array("website"=>"Jaischool.com","hosting"=>"Siteground","plan"=>"GoGeek");
$array = array_keys($assoc_array);
echo $array[0];
?>
Print all keys name of an associative array by loop
To print all the key's names from an associative at once array you need to run a loop that will last till the length of the array. Use sizeof() function to get the length of the array.
<?php
$assoc_array = array("website"=>"Jaischool.com","hosting"=>"Siteground","plan"=>"GoGeek");
$array = array_keys($assoc_array);
$length = sizeof($assoc_array);
for($i=0;$i<$length;$i++)
{
echo $array[$i]."<br>";
}
?>
Print all keys of an associative array by indexing number
<?php
$assoc_array = array("website"=>"Jaischool.com","hosting"=>"Siteground","plan"=>"GoGeek");
$array = array_keys($assoc_array);
echo $array[0]." = ".$assoc_array[$array[0]];
?>
Print all keys and their value from an associative array by loop
<?php
$assoc_array = array("website"=>"Jaischool.com","hosting"=>"Siteground","plan"=>"GoGeek");
$array = array_keys($assoc_array);
$length = sizeof($assoc_array);
for($i=0;$i<$length;$i++)
{
$index_num = $array[$i];
echo $array[$i]." - ".$assoc_array[$index_num]."<br>";
}
?>
Publish A Comment