Get key of first item in array
Moderator: General Moderators
Get key of first item in array
Is there a function to just get the key value of the first element in an array? Thanks!
Re: Get key of first item in array
http://php.net/manual/en/function.array-shift.php
but it will remove the first element
but it will remove the first element
There are 10 types of people in this world, those who understand binary and those who don't
Re: Get key of first item in array
Code: Select all
reset($array);
echo key($array);
Re: Get key of first item in array
That gives me the value of the first element thought... I want the key of the first element
Re: Get key of first item in array
Perfect, thank you!Weirdan wrote:Code: Select all
reset($array); echo key($array);
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Re: Get key of first item in array
Just be careful about the side-effects of reset() (usually not a problem)
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: Get key of first item in array
I believe array_keys() gives you all of the keys as an array as well, so you reference the key using that array as well.
Re: Get key of first item in array
OpsGeXus wrote:That gives me the value of the first element thought... I want the key of the first element
There are 10 types of people in this world, those who understand binary and those who don't
- jimthunderbird
- Forum Contributor
- Posts: 147
- Joined: Tue Jul 04, 2006 3:59 am
- Location: San Francisco, CA
Re: Get key of first item in array
I will use this approach.Everah wrote:I believe array_keys() gives you all of the keys as an array as well, so you reference the key using that array as well.