How to get numeric index of associative array in PHP?

In PHP we can associate name/label with each array elements using => symbol. This is very helpful as it is easy to remember the element because each element is represented by the label rather than the index value.
Using array_keys() function: The array_keys() function is an inbuilt function in PHP which is used to return either all the keys of an array or the subset of the keys.

Syntax:

array array_keys( $input_array, $search_value, $strict )

Program 1: Program to get numeric index of associative array using array_keys() function.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Program to print index of an associative array
  
// Declare an associative array
$assoc_array=array("Geeks"=>10, "for"=>15, "geeks"=>20); 
  
// Print index with corresponding key
// using array_keys() function
print_r(array_keys($assoc_array));
  
?>

chevron_right


Example 2: Below program uses index to access the values in associative array.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Program to print values using index
// of associative array
  
// Declare an associative array
$assoc_array = array(
    "Geeks" => 30,
    "for" => 20,
    "geeks" => 10
); 
  
// Using array_keys() function
$key = array_keys($assoc_array);
  
// Calculate size of array
$size = sizeof($key);
  
// Using loop to access values
for( $i = 0; $i < $size; $i++) {
    echo "${assoc_array[$key[$i]]}\n";
}
  
?>

chevron_right




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.