Skip to content
Related Articles

Related Articles

How to get numeric index of associative array in PHP?
  • Last Updated : 30 Apr, 2019

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.


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

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

// 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

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

// 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";

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :