Get first key in a (possibly) associative array?

Viewed 635740

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?

25 Answers

2019 Update

Starting from PHP 7.3, there is a new built in function called array_key_first() which will retrieve the first key from the given array without resetting the internal pointer. Check out the documentation for more info.


You can use reset and key:

reset($array);
$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)

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.

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.

For 2018+

Starting with PHP 7.3, there is an array_key_first() function that achieve exactly this:

$array = ['foo' => 'lorem', 'bar' => 'ipsum'];
$firstKey = array_key_first($array); // 'foo'

Documentation is available here.

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

Since PHP 7.3.0 function array_key_first() can be used.

There are several ways to provide this functionality for versions prior to PHP 7.3.0. It is possible to use array_keys(), but that may be rather inefficient. It is also possible to use reset() and key(), but that may change the internal array pointer. An efficient solution, which does not change the internal array pointer, written as polyfill:

<?php
if (!function_exists('array_key_first')) {
    function array_key_first(array $arr) {
        foreach($arr as $key => $unused) {
            return $key;
        }

        return null;
    }
}
?>

Re the @Blixt answer, prior to 7.3.0, this polyfill can be used:

if (!function_exists('array_key_first')) {
  function array_key_first(array $array) {
    return key(array_slice($array, 0, 1, true));
  }
}

This will work on all PHP versions

$firstKey = '' ;

//$contact7formlist - associative array. 

if(function_exists('array_key_first')){
    
    $firstKey = array_key_first($contact7formlist);
    
}else{
    
    foreach ($contact7formlist as $key => $contact7form ){
        $firstKey = $key;
        break;
    }
}

I think the best and fastest way to do it is:

$first_key=key(array_slice($array, 0, 1, TRUE))

array_chunk split an array into chunks, you can use:

$arr = ['uno'=>'one','due'=>'two','tre'=>'three'];
$firstElement = array_chunk($arr,1,true)[0];
var_dump($firstElement);

use :

$array = ['po','co','so'];

echo reset($array); 

Result : po

Related