Get first key in a (possibly) associative array?

0 votes
asked Jun 22, 2009 by alex-s

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?

18 Answers

0 votes
answered by salvi-pascual

This is the easier way I had ever found. Fast and only two lines of code :-D

$keys = array_keys($array);
echo $array[$keys[0]];
0 votes
answered Jun 22, 2009 by troelskn

array_keys returns an array of keys. Take the first entry. Alternatively, you could call reset on the array, and subsequently key. The latter approach is probably slightly faster (Thoug I didn't test it), but it has the side effect of resetting the internal pointer.

0 votes
answered Jun 22, 2009 by blixt

You can use reset and key:

$first_key = key($array);

It's essentially the same as your initial code, but with a little less overhead, and it's more obvious what is happening.

Just remember to call reset, or you may get any of the keys in the array. You can also use end instead of reset to get the last key.

If you wanted the key to get the first value, reset actually returns it:

$first_value = reset($array);

There is one special case to watch out for though (so check the length of the array first):

$arr1 = array(false);
$arr2 = array();
var_dump(reset($arr1) === reset($arr2)); // bool(true)
0 votes
answered Jun 22, 2009 by jimyi

key($an_array) will give you the first key

edit per Blixt: you should call reset($array); before key($an_array) to reset the pointer to the beginning of the array.

0 votes
answered Jun 2, 2011 by pupil

Please find the following:

$yourArray = array('first_key'=> 'First', 2, 3, 4, 5);
$keys   =   array_keys($yourArray);
echo "Key = ".$keys[0];

I think this will work.

0 votes
answered Jun 12, 2012 by serg
list($firstKey) = array_keys($yourArray);
0 votes
answered Jun 28, 2012 by hamidreza
$myArray = array(
    2 => '3th element',
    4 => 'first element',
    1 => 'second element',
    3 => '4th element'
echo min(array_keys($myArray)); // return 1
0 votes
answered Jun 28, 2012 by pupil

This could also be a solution.

$first_key = current(array_flip($array));

I have tested it and it works.

0 votes
answered Jun 25, 2013 by martin-vseticka

If efficiency is not that important for you, you can use array_keys($yourArray)[0] in PHP 5.4 (and higher).


# 1
$arr = ["my" => "test", "is" => "best"];    
echo array_keys($arr)[0] . "\r\n"; // prints "my"

# 2
$arr = ["test", "best"];
echo array_keys($arr)[0] . "\r\n"; // prints "0"

# 3
$arr = [1 => "test", 2 => "best"];
echo array_keys($arr)[0] . "\r\n"; // prints "1"

The advantage over solution:

list($firstKey) = array_keys($yourArray);

is that you can pass array_keys($arr)[0] as a function parameter (i.e. doSomething(array_keys($arr)[0], $otherParameter)).


0 votes
answered Jun 6, 2014 by stopper

You could try

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter