When navigating through array with next()
and prev()
, how could you get the current key the array is at?
You can use the key
function :
key() returns the index element of the current array position.
And, as a quick example, you can consider this portion of code :
$array = array(
'first' => 123,
'second' => 456,
'last' => 789,
);
reset($array); // Place pointer on the first element
next($array); // Advance to the second one
$key = key($array); // Get the key of the current (i.e. second) element
var_dump($key);
It'll output, as expected, the key of the second element :
string 'second' (length=6)
Use the key
function to get the key of the item the internal pointer is currently pointing to.
You probably want key().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With