Two ways to find the subscript of the first element of an array in php
Method 1: Use key() to get the subscript of the first element of the specified array
key() can return the key name of the current element in the array; initially, the current element points to the first element.
<?php
header("Content-type:text/html;charset=utf-8");
$arr=array("a"=>"1","b"=>"","c"=>"2","d"=>0,"e"=>"blue");
echo "Original array: ";
var_dump($arr);
echo "The subscript of the first element of the array is: ".key($arr) ;
?>
Method 2: Use array_keys() and “key name array name [0]” to get the subscript of the first element of the specified array
The array_keys() function can get all the subscripts of the original array and return an array of key names containing all the subscripts
Use “$ key name array name [0]” to get the first element of the key name array, that is, the subscript of the first element of the original array
<?php
header("Content-type:text/html;charset=utf-8");
$arr=array("a"=>"1","b"=>"","c"=>"2","d"=>0,"e"=>"blue");
echo "Original array: ";
var_dump($arr);
$keys=array_keys($arr);
echo "Array of key names: ";
var_dump($keys);
echo "The subscript of the first element of the original array is: ".$keys[0] ;
?>