Get the value of the first element of an unknown character key an array group by using the PHP current () function
In development often encountered such a problem, get the value of the first element of the array, if it is a digital index that is OK, direct $array[0], if the key name is a string, you do not know this string? You can do it with the current () function.
Of course, you can use the Array_shift () function, but it will break the original array (that is, delete the first element in the array and return the value of the deleted element).
About the current () function:
Each array has an internal pointer to its " current " cell, which initially points to the first cell inserted into the array. Obtained with current ().
Similar functions:
End () Moves the internal pointer of the array to the last cell and returns its value.
Next () returns the value of the next cell pointed to by the array's internal pointer, or FALSE if there are no more cells.
Prev () returns the value of the previous cell pointed to by the array's internal pointer, or FALSE if there are no more cells.
Reset () returns the internal pointer of the array back to the first cell and returns the value of the first array cell, False if the array is empty.
<?php
$arr = Array ("A" = "PHP", "Java", "C");
Echo current ($arr); //php
Echo Next ($arr); Java
Echo prev ($arr); PHP points to the value of the previous cell, so it's PHP again.
Echo End ($arr); C
?>
Get the value of the first element of an unknown character key an array group by using the PHP current () function