rfc:array_key_first_last
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:array_key_first_last [2018/06/11 14:48] – created wol-soft | rfc:array_key_first_last [2018/07/18 13:06] (current) – add references wol-soft | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: array_key_first() | + | ====== PHP RFC: array_key_first(), array_key_last() and array_value_first(), |
- | * Version: 1.0 | + | * Version: 1.1 |
* Date: 2018-06-11 | * Date: 2018-06-11 | ||
* Author: Enno Woortmann< | * Author: Enno Woortmann< | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | As arrays are a powerful data structure it's handy in some cases to get the first or the last key of an array without a workaround. To accomplish this task this RFC adds two functions to the core: | + | As arrays are a powerful data structure it's handy in some cases to get the first or the last key/ |
* $key = array_key_first($array); | * $key = array_key_first($array); | ||
* $key = array_key_last($array); | * $key = array_key_last($array); | ||
+ | * $value = array_value_first($array); | ||
+ | * $value = array_value_last($array); | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | The current functions of the PHP core only allow to retrieve the first/last key 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], | + | The current functions of the PHP core only allow to retrieve the first/last key/ |
- | 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 | + | 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 **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. | + | 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: | ||
<code php> | <code php> | ||
Line 49: | Line 53: | ||
</ | </ | ||
- | The functions either return the requested key or null if an empty array is provided. | + | 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: | ||
+ | |||
+ | <code php> | ||
+ | // usage of an associative array | ||
+ | $array = [' | ||
+ | |||
+ | $firstValue = array_value_first($array); | ||
+ | $lastValue = array_value_last($array); | ||
+ | |||
+ | assert($firstValue === 1); | ||
+ | assert($lastValue === 3); | ||
+ | |||
+ | // usage of a numeric array | ||
+ | $array = [1 => ' | ||
+ | |||
+ | $firstValue = array_value_first($array); | ||
+ | $lastValue = array_value_last($array); | ||
+ | |||
+ | assert($firstValue === ' | ||
+ | assert($lastValue === ' | ||
+ | |||
+ | // 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/ | ||
+ | If a non array parameter is given, a warning will be triggered and null will be returned. | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 58: | Line 98: | ||
===== RFC Impact ===== | ===== RFC Impact ===== | ||
- | As this RFC adds two 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. | + | 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. | There is no impact to SAPIs or the opcache. | ||
- | ===== Proposed Voting Choices | + | ===== Vote ===== |
- | As this RFC doesn' | + | This is not a language |
+ | |||
+ | Voting starts on 2018-07-09 13:30 UTC and closes on 2018-07-16 23:00 UTC. | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
Line 69: | Line 123: | ||
===== Implementation ===== | ===== Implementation ===== | ||
- | After the project is implemented, | + | |
- | - the version(s) it was merged | + | The implementation for array_key_first() and array_key_last() was merged |
- | - a link to the git commit(s) | + | |
- | | + | The documentation |
- | - a link to the language specification section (if any) | + | |
+ | | ||
+ | * http:// | ||
+ | |||
+ | ===== 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. | ||
rfc/array_key_first_last.1528728536.txt.gz · Last modified: 2018/06/11 14:48 by wol-soft