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" ; }   ?> |