In PHP, array_key_first () gets the first key of an array however, it does not affect the internal array pointer. It was introduced in PHP 7.3.0. 

Parameters

array

As has been noted. The parameter should be an array and it is compulsory.

Return Values

array_key_first( ) function returns the first key of the array. Moreover, if the array isn’t blank, otherwise, it’s null.

Example 

<?php

$farray = [‘a’ => 1, ‘b’ => 2, ‘c’ => 3];

$fKey = array_key_first($farray);

var_dump($fKey);

?>

Output

string(1) “a”

Tip

There are many ways to explain this functionality for versions before PHP7.3.0. Also, It can be possible to use array_key(), however, that might be not efficient. This is possible to use key() and reset() as well however it might get change the internal array pointer. A perfect solution which doesn’t change the internal array pointer is mentioned below:

<?php

if (!function_exists(‘array_key_first’)) {

    function array_key_first(array $arr) {

        foreach($arr as $key => $unused) {

            return $key;

        }

        return NULL;

    }

}

?>

To know about the latest updates of PHP you can also visit here :

If you have any questions or queries you may also refer the website link from here

Contributor
Comments to: In PHP, array_key_first() gets the first key of an array

Your email address will not be published. Required fields are marked *

19 + nine =

Attach images - Only PNG, JPG, JPEG and GIF are supported.

Login

Welcome to Phpblogs

Brief and amiable onboarding is the first thing a new user sees in Phpblogs.
Join Phpblogs.
Registration is closed.