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 :

0 Shares:
Leave a Reply

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

You May Also Like

Dix Breves Rencontres Ebook Gratuit

Pour votre bien-être et pour arriver à vos fins, rencontres gratuites – https://www.paparencontres.fr/rencontres-gratuites/ – il est indispensable de…