When navigating through array with next()
and prev()
, how could you get the current key the array is at?
views:
77answers:
3
+6
A:
Use the key
function to get the key of the item the internal pointer is currently pointing to.
Gumbo
2010-02-27 17:29:24
How dare you beat me by 47 seconds? +1
Billy ONeal
2010-02-27 17:30:44
@BillyONeal: Moderators can travel back in time. ;-)
Gumbo
2010-02-27 17:32:35
Well whoever was first should get the best answer. You're all right, obviously.
Brian Lacy
2010-02-27 17:34:41
Agreed Brian Lacy. Just was trying to make a joke... failed miserably at that apparently.
Billy ONeal
2010-02-27 17:35:44
@BillONeal: Fear not, I caught the jest, really was just commenting on the fact that three people gave the same correct answer in a matter of like two minutes. Of course it helps that the question was a simple one!
Brian Lacy
2010-03-01 14:18:23
+9
A:
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)
Pascal MARTIN
2010-02-27 17:30:08
Well, I suppose I don't have the ability to travel back in time... But I suppose I pressed F5 a couple of seconds before you did ^^
Pascal MARTIN
2010-02-27 17:34:59