Friday, October 17, 2025
HomeLanguagesHow to get numeric index of associative array in PHP?

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.




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


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




<?php
  
// Program to print values using index
// of associative array
  
// Declare an associative array
$assoc_array = array(
    "Geeks" => 30,
    "for" => 20,
    "neveropen" => 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";
}
  
?>


Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS