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.
Syntax:
array array_keys( $input_array, $search_value, $strict )
Program 1: Program to get numeric index of associative array using array_keys() function.
<?php
$assoc_array = array ( "Geeks" =>10, "for" =>15, "geeks" =>20);
print_r( array_keys ( $assoc_array ));
?>
|
Example 2: Below program uses index to access the values in associative array.
<?php
$assoc_array = array (
"Geeks" => 30,
"for" => 20,
"geeks" => 10
);
$key = array_keys ( $assoc_array );
$size = sizeof( $key );
for ( $i = 0; $i < $size ; $i ++) {
echo "${assoc_array[$key[$i]]}\n" ;
}
?>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...