How to get the key of the current array element?

When navigating an array using next() and prev() , how could you get the current key that is in the array?

+4
source share
3 answers

You can use the key function:

key () returns the index element of the current position of the array.

And, as a quick example, you can consider this part of the 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 (ie second) element var_dump($key); 

It will output, as expected, the key of the second element:

 string 'second' (length=6) 
+13
source

Use the key function to get the key of the element that the internal pointer points to.

+7
source

You probably want key () .

+4
source

Source: https://habr.com/ru/post/1302604/


All Articles