Take the State of PHP 2026 Survey →

array_key_first(), array_key_last() View as Markdown

Implemented (PHP 7.3)
Authors
Enno Woortmannenno.woortmann@web.de
Date
2018-06-11
Version
1.1
Source
wiki.php.net/rfc/array_key_first_last

Votes

Add array_key_first() and array_key_last()? closed
Yes
18
No
14
32 voters
ajf Yes 2018-07-09 15:58
ashnazg Yes 2018-07-09 16:24
bwoebi No 2018-07-11 06:59
cmb Yes 2018-07-09 12:00
colinodell No 2018-07-11 16:19
dams Yes 2018-07-09 11:40
danack No 2018-07-10 14:30
derick No 2018-07-16 11:53
hywan Yes 2018-07-11 13:16
jhdxr No 2018-07-09 17:21
jwage Yes 2018-07-10 00:41
kalle Yes 2018-07-09 15:17
kelunik No 2018-07-15 13:58
kguest Yes 2018-07-09 13:25
krakjoe No 2018-07-11 16:47
lbarnaud Yes 2018-07-09 18:44
lcobucci No 2018-07-12 07:24
levim No 2018-07-09 14:47
lex Yes 2018-07-09 14:05
mcmic Yes 2018-07-10 12:35
narf Yes 2018-07-09 12:26
ocramius Yes 2018-07-09 11:20
peehaa No 2018-07-10 17:31
pmmaga No 2018-07-10 18:22
pollita No 2018-07-13 17:47
rtheunissen No 2018-07-09 17:46
stas Yes 2018-07-10 00:54
trowski No 2018-07-11 16:55
weierophinney Yes 2018-07-09 16:45
yohgaki Yes 2018-07-11 21:42
zeev Yes 2018-07-12 08:21
zimt Yes 2018-07-13 11:29
Add array_value_first() and array_value_last()? closed
Yes
15
No
18
33 voters
ajf No 2018-07-11 23:02
ashnazg Yes 2018-07-09 16:24
bwoebi No 2018-07-11 06:59
cmb No 2018-07-12 22:08
colinodell No 2018-07-11 16:19
dams Yes 2018-07-09 11:40
danack No 2018-07-10 14:31
derick No 2018-07-16 11:53
hywan Yes 2018-07-11 13:16
jhdxr No 2018-07-09 17:21
jwage Yes 2018-07-10 00:41
kalle Yes 2018-07-09 15:17
kelunik No 2018-07-15 13:58
kguest Yes 2018-07-09 13:25
krakjoe No 2018-07-11 16:50
lbarnaud Yes 2018-07-09 18:44
lcobucci No 2018-07-12 07:25
levim No 2018-07-09 14:47
lex Yes 2018-07-09 14:05
mcmic Yes 2018-07-10 12:35
nikic No 2018-07-13 21:54
ocramius Yes 2018-07-09 11:20
peehaa No 2018-07-10 17:31
pmmaga No 2018-07-10 18:22
pollita No 2018-07-10 13:25
rtheunissen No 2018-07-09 17:47
stas Yes 2018-07-10 00:54
svpernova09 Yes 2018-07-13 17:56
trowski No 2018-07-11 16:55
weierophinney Yes 2018-07-09 16:45
yohgaki Yes 2018-07-11 21:42
zeev Yes 2018-07-12 08:22
zimt No 2018-07-13 11:29

Discussions on the mailing list

Two new functions array_first() and array_last()
by David Grudl · 46 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

As arrays are a powerful data structure it's handy in some cases to get the first or the last key/value of an array without a workaround. To accomplish this task this RFC adds four functions to the core:

  • $key = array_key_first($array);
  • $key = array_key_last($array);
  • $value = array_value_first($array);
  • $value = array_value_last($array);

Proposal

The current functions of the PHP core only allow to retrieve the first/last key/value of an array by changing the internal state of the array when using the functions reset(), end() and key(). Other implementation approaches are either inperformant (eg. usage of array_keys($array)[0] to gather the first key, which additionally may lead to errors if $array is an empty array) or provide a construct which increases the cognitive complexity of the implementation (eg. usage of loops).

To avoid changes of the internal array representation in order to gather a single information from the array and to increase the comprehensibility of the userland code this RFC proposes new functions for this task. Two functions for the handling of array keys and two functions for the handling of array values:

To gather the key of the first element of an array the method array_key_first($array) will be added.

To gather the key of the last element of an array the method array_key_last($array) will be added.

The usage of the methods to handle the keys will look like:

// usage of an associative array
$array = ['a' => 1, 'b' => 2, 'c' => 3];
 
$firstKey = array_key_first($array);
$lastKey = array_key_last($array);
 
assert($firstKey === 'a');
assert($lastKey === 'c');
 
// usage of a numeric array
$array = [1 => 'a', 2 => 'b', 3 => 'c'];
 
$firstKey = array_key_first($array);
$lastKey = array_key_last($array);
 
assert($firstKey === 1);
assert($lastKey === 3);
 
// usage of an empty array
$array = [];
 
$firstKey = array_key_first($array);
$lastKey = array_key_last($array);
 
assert($firstKey === null);
assert($lastKey === null);

To gather the value of the first element of an array the method array_value_first($array) will be added.

To gather the value of the last element of an array the method array_value_last($array) will be added.

The usage of the methods to handle the values will look like:

// usage of an associative array
$array = ['a' => 1, 'b' => 2, 'c' => 3];
 
$firstValue = array_value_first($array);
$lastValue = array_value_last($array);
 
assert($firstValue === 1);
assert($lastValue === 3);
 
// usage of a numeric array
$array = [1 => 'a', 2 => 'b', 3 => 'c'];
 
$firstValue = array_value_first($array);
$lastValue = array_value_last($array);
 
assert($firstValue === 'a');
assert($lastValue === 'c');
 
// usage of an empty array
$array = [];
 
$firstValue = array_value_first($array);
$lastValue = array_value_last($array);
 
assert($firstValue === null);
assert($lastValue === null);

All four functions either return the requested key/value or null if an empty array is provided. If a non array parameter is given, a warning will be triggered and null will be returned.

Backward Incompatible Changes

None.

Proposed PHP Version(s)

next PHP 7.x

RFC Impact

As this RFC adds four new functions there is a possible impact on existing userland extensions where developers created helper functions with an identical name. As the functions proposed provide a meaningful name custom functions should implement an identical functionality and thus be replaceable.

There is no impact to SAPIs or the opcache.

Vote

This is not a language change so a simple yes/no vote with 50%+1 majority is required.

Voting starts on 2018-07-09 13:30 UTC and closes on 2018-07-16 23:00 UTC.

Vote: Add array_key_first() and array_key_last()? — Yes: 18 · No: 14 (closed)


Vote: Add array_value_first() and array_value_last()? — Yes: 15 · No: 18 (closed)

Patches and Tests

The GitHub pull request which provides the functionality and the related tests is located at https://github.com/php/php-src/pull/3256

Implementation

The implementation for array_key_first() and array_key_last() was merged for PHP 7.3 with the commit http://git.php.net/?p=php-src.git;a=commit;h=50516a6

The documentation for the added functions is loated at:

History

Version 1.1

  • Extended the scope of this RFC to also cover functions for the handling of values to provide a complete function set for working with outer array elements.