# RFC: array_key_first(), array_key_last()

- Status: Implemented (PHP 7.3)
- Authors: Enno Woortmannenno.woortmann@web.de
- Date: 2018-06-11
- Version: 1.1
- Original: https://wiki.php.net/rfc/array_key_first_last
- Page: https://externals.io/rfc/array_key_first_last
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Add array_key_first() and array_key_last()? (closed)

Yes: 18 · No: 14

Voters: ajf (Yes), ashnazg (Yes), bwoebi (No), cmb (Yes), colinodell (No), dams (Yes), danack (No), derick (No), hywan (Yes), jhdxr (No), jwage (Yes), kalle (Yes), kelunik (No), kguest (Yes), krakjoe (No), lbarnaud (Yes), lcobucci (No), levim (No), lex (Yes), mcmic (Yes), narf (Yes), ocramius (Yes), peehaa (No), pmmaga (No), pollita (No), rtheunissen (No), stas (Yes), trowski (No), weierophinney (Yes), yohgaki (Yes), zeev (Yes), zimt (Yes)

### Add array_value_first() and array_value_last()? (closed)

Yes: 15 · No: 18

Voters: ajf (No), ashnazg (Yes), bwoebi (No), cmb (No), colinodell (No), dams (Yes), danack (No), derick (No), hywan (Yes), jhdxr (No), jwage (Yes), kalle (Yes), kelunik (No), kguest (Yes), krakjoe (No), lbarnaud (Yes), lcobucci (No), levim (No), lex (Yes), mcmic (Yes), nikic (No), ocramius (Yes), peehaa (No), pmmaga (No), pollita (No), rtheunissen (No), stas (Yes), svpernova09 (Yes), trowski (No), weierophinney (Yes), yohgaki (Yes), zeev (Yes), zimt (No)

## Discussions on the mailing list

- [\[RFC\]\[Under Discussion\] Add functions array_key_first() and array_key_last()](https://externals.io/message/102245.md) — started on 2018-06-13, 36 messages
- [\[VOTE\] array_key_first(), array_key_last(), array_value_first(), array_value_last()](https://externals.io/message/102663.md) — started on 2018-07-09, 34 messages
- [Two new functions array_first() and array_last()](https://externals.io/message/121313.md) — started on 2023-10-14, 46 messages

---

## 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:

```php
// 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:

```php
// 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:

- <http://php.net/manual/en/function.array-key-first.php>
- <http://php.net/manual/en/function.array-key-last.php>

## 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.
