Although
array_keys() can be used to get an indexed array of keys for an associative array. Since the resulting array is indexed, the elements of the resulting array can be accessed by an integer index. Using this resulting array, the keys of the original array can be accessed by an integer index, and then the keys can be used to access the elements of the original array. Thus, using an integer index, you can access the elements of the original array using an optional indexed array of keys.
array_keys() function:array_keys() function takes an array as input and returns an indexed array that only consists of the keys of the original array, indexed where indexing starts at zero.
Syntax:array array_keys ($arr )
Parameters:The array_keys() function takes an array as input and uses only the array keys to create the resulting indexed array.
Note.The array_keys() function does not change the order of the keys in the original array. If an indexed array is passed, the resulting array will have integers as its value.
Program:PHP program for accessing an associative array using an integer index.
php
// PHP program for accessing associative
// array by integer index
/ / Example associative array
$arr
=
array
(
’one’
= >
’engineer’
,
’two’
= >
’for’
,
’three’
= >
’engineer’
);
// Getting keys from $arr
// using the array_keys() function
$keys
=
array_keys
(
$arr
);
echo
" The keys array: "
;
print_r (
$keys
);
// Get the size of the sample array
$size
= sizeof (
$arr
);
// Access $arr elements using
// integer index using $x
echo
"The elements of the sample array:"
.
""
;
for
(
$x
= 0;
$x
<
$size
;
$x
++) {
echo
"key:"
.
$keys
[
$x
].
", value:"
.
$arr
[
$keys
[
$x
]].
""
;
}
?>
Exit:The keys array : Array ([0] = > one [1] = > two [2] = > three) The elements of the sample array: key: one, value: engineer key: two, value: for key: three, value: engineer