How do I get the current index in a foreach
loop?
foreach ($arr as $key => $val)
{
// How do I get the index?
// How do I get the fir
You could get the first element in the array_keys() function as well. Or array_search() the keys for the "index" of a key. If you are inside a foreach
loop, the simple incrementing counter (suggested by kip or cletus) is probably your most efficient method though.
'something', 'test2'=>'something else');
$keys = array_keys($array);
var_dump(array_search("test2", $keys)); // int(1)
var_dump(array_search("test3", $keys)); // bool(false)