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?
Please find the following:
I think this will work.
php73:
http://php.net/manual/en/function.array-key-first.php
You can play with your array
To get the first element of array you can use
current
and for last element you can useend
Edit
Just for the sake for not getting any more down votes for the answer you can convert you key to value using
array_keys
and use as shown above.Today I had to search for the first key of my array returned by a POST request. (And note the number for a form id etc)
Well, I've found this: Return first key of associative array in PHP
http://php.net/key
I've done this, and it work.
Maybe it will eco 15min of an other guy. CYA.
A one-liner: