Pick first or last array keys with build-in PHP 7.3 new functions

Pick first or last array keys with build-in PHP 7.3 new functions

Get array first and last keys in PHP 7.3

There is finally some useful 7.3 features for arrays! Introducing array_key_first() and array_key_last().

There are new functions added to array package: array_key_first() and array_key_last() for getting the first or last key of the array. This is extremely useful when the array is not an index based one and we don’t know its keys. These functions will not affect the internal array pointer.

$array = ['a' => 1, 'b' => 2, 'c' => 3];
 
echo array_key_first($array);
echo array_key_last($array);

//will return
a
c

Reference

PHP 7 News & Updates v7.0 - 7.4 - Book coverPHP 7 News & Updates v7.0 – 7.4 Book https://www.amazon.com/PHP-News-Updates-v7-0-7-4/dp/1727202481/

Rate this artcile
[Total: 1 Average: 5]

2 thoughts on “Pick first or last array keys with build-in PHP 7.3 new functions

Comments are closed.