array_key_first

(PHP 7 >= 7.3.0)

array_key_firstGets the first key of an array

Description

array_key_first ( array $array ) : mixed

Get the first key of the given array without affecting the internal array pointer.

Parameters

array

An array.

Return Values

Returns the first key of array if the array is not empty; NULL otherwise.

Examples

Example #1 Basic array_key_first() Usage

<?php
$array 
= ['a' => 1'b' => 2'c' => 3];

$firstKey array_key_first($array);

var_dump($firstKey);
?>

The above example will output:

string(1) "a"

See Also

  • array_key_last() - Gets the last key of an array
  • reset() - Set the internal pointer of an array to its first element
add a note add a note

User Contributed Notes 5 notes

up
12
mirko dot pagliai at gmail dot com
3 months ago
It's much simpler than the examples already posted by other users (without ridiculous foreach cycles, LoL).

For PHP < 7.3.0:

<?php
if (!function_exists('array_key_first')) {
   
/**
     * Gets the first key of an array
     *
     * @param array $array
     * @return mixed
     */
   
function array_key_first(array $array)
    {
        return
$array ? array_keys($array)[0] : null;
    }
up
3
devonblzx
9 days ago
Since the other notes for polyfills did not account for the internal pointer, empty arrays, or pre-PHP7, this polyfill should work reliably for all PHP5 and PHP7 versions:

<?php
if (!function_exists('array_key_first')) {
   
/**
     * Gets the first key of an array
     *
     * @param array $array
     * @return mixed
     */
   
function array_key_first($array)
    {
        if (!
is_array($array) || !count($array)) {
             return
null;
        }
       
$keys = array_keys($array);
        return
$keys[0];
    }
}
up
11
mail at vitalyart dot ru
5 months ago
For PHP < 7.3.0:

<?php
if (!function_exists('array_key_first')) {
   
/**
     * Gets the first key of an array
     *
     * @param array $array
     * @return mixed
     */
   
function array_key_first(array $array)
    {
        if (
count($array)) {
           
reset($array);
            return
key($array);
        }

        return
null;
    }
}

echo
array_key_first([
   
'foo' => 'value1',
   
'bar' => 'value2',
]);

// Output: foo
up
0
divinity76+spam at gmail dot com
6 days ago
for <7.3.0

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

- solution of "mirko dot pagliai at gmail" is slow, solution from "mail at vitalyart dot ru " edits the internal array pointer, the solution from "devonblzx" is slow, and the solution from "manzoorwanijk" is.. ok i guess, but this is better.
up
-4
@manzoorwanijk
5 months ago
For PHP < 7.3.0 :

<?php
if ( ! function_exists( 'array_key_first' ) ) {
   
/**
     * Polyfill for array_key_first() function added in PHP 7.3.
     *
     * Get the first key of the given array without affecting
     * the internal array pointer.
     *
     * @param mixed $array An array
     *
     * @return mixed The first key of array if the array is not empty; NULL otherwise.
     */
   
function array_key_first( $array ) {
       
$key = null;

        if (
is_array( $array ) ) {

            foreach (
$array as $key => $value ) {
                break;
            }
        }

        return
$key;
    }
}
?>
To Top