If I have an associative array:
$arr = array(
1 => "Value1",
2 => "Value2",
10 => "Value10"
);
then I want to get the keys "1", "2" and "10" of the array $arr?
there are may approach to do first we should use foreach() loop with key and value then echo the keys,
foreach ($arr as $key => $value) {
echo $key;
}
Another use array_keys(), PHP will give you an array filled with just the keys:
$keys = array_keys($arr);
foreach($keys as $key) {
echo($key);
}
#complete_example.php
<?php
$arr = array(
1 => "Value1",
2 => "Value2",
10 => "Value10"
);
foreach ($arr as $key => $value) {
echo $key; //output 1 2 10
}
?>
Thanks for visiting us!