Get first key in a (possibly) associative array?
Asked 07 September, 2021
Viewed 2.8K times
  • 63
Votes

What's the best way to determine the first key in a possibly associative array? My first thought it to just foreach the array and then immediately breaking it, like this:

foreach ($an_array as $key => $val) break;

Thus having $key contain the first key, but this seems inefficient. Does anyone have a better solution?

23 Answer