How to print array index? [closed]
I want to print the array index for some purpose. Can anybody tell me about this ?
Get the first index of an elemnt:
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
echo $key;
For all elemnts
foreach($array as $key => $value){
echo "{$key} => {$value}\n";
}
will output
0 => blue
1 => red
2 => green
3 => red
All keys:
echo implode(', ',array_keys($array));
will output
0, 1, 2, 3
not sure what you mean with this question but you could try
echo "<pre>";
print_r ($arr);
echo "</pre>";
that way you'll see your entire array including all the indexes
if you mean the keys of the array:
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));
//result
Array
(
[0] => 0
[1] => color
)
This will provide an array of the keys
精彩评论