rfc:array_key_first_last_index
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:array_key_first_last_index [2015/12/07 17:07] – created jbafford | rfc:array_key_first_last_index [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: array_key_(first|last|index) ====== | ====== PHP RFC: array_key_(first|last|index) ====== | ||
- | * Version: 1.0 | + | * Version: 1.0.1 |
- | * Date: 2015-12-05 | + | * Date: 2016-01-01 |
* Author: John Bafford | * Author: John Bafford | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | This RFC and its accompanying patch add the following functions: | + | Sometimes, it is necessary to retrieve the first or last key of an array, but there is no convenient means of doing so currently provided by the language without modifying the source array or resorting to odd-to-read code. This RFC and its accompanying patch add the following functions |
* $key = array_key_first($array [, & | * $key = array_key_first($array [, & | ||
Line 15: | Line 15: | ||
===== Proposal ===== | ===== Proposal ===== | ||
+ | Arrays in php are ordered maps, but there is no convenient way to get the first or last key of an array (a common function in some use cases), or even an arbitrary key by index (a fairly rare need). | ||
+ | |||
array_key_first() returns the first key in an array. | array_key_first() returns the first key in an array. | ||
array_key_last() returns the last key in an array. | array_key_last() returns the last key in an array. | ||
- | array_key_index() returns the key at a specified index (using a substr-like offset; | + | array_key_index() returns the key at a specified index (using a substr-like offset; |
- | All three functions return the requested array key, or null if the requested array key is not found (an empty array, or an out-of-bounds index in the case of array_key_index). | + | <code php> |
+ | $arr [' | ||
+ | |||
+ | $key = array_key_first($arr); | ||
+ | |||
+ | $key = array_key_last($arr, | ||
+ | |||
+ | $key = array_key_index($arr, | ||
+ | $key = array_key_index($arr, | ||
+ | $key = array_key_index($arr, | ||
+ | |||
+ | |||
+ | // Also works for numeric arrays | ||
+ | $numeric = [1 => 1, 5 => 2, 2 => 3]; | ||
+ | $key = array_key_index($numeric, | ||
+ | $key = array_key_index($numeric, | ||
+ | $key = array_key_index($numeric, | ||
+ | </ | ||
+ | |||
+ | |||
+ | All three functions return the requested array key, or null if the requested array key is not found (an empty array, or an out-of-bounds index in the case of array_key_index). | ||
Line 35: | Line 57: | ||
* Retrieving the last key can be done by either of these more complicated/ | * Retrieving the last key can be done by either of these more complicated/ | ||
* foreach(array_reverse($arr) as $key => $value) { break; } | * foreach(array_reverse($arr) as $key => $value) { break; } | ||
- | * foreach($arr as $key => $value); //Iterate over the entire array; as a possibly unexpected | + | * foreach($arr as $key => $value); //Iterate over the entire array, doing nothing, just for the side-effect |
- | * reset($arr); | + | * reset($arr); |
+ | * end($arr); $key = key($arr); | ||
* This modifies the array (resets the array pointer). | * This modifies the array (resets the array pointer). | ||
- | * There is no simple way to get either the last key or an indexed key via this method. | ||
+ | With the reference implementation provided in the patch referenced below, array_key_first() and array_key_last() directly retrieve the array key from the underlying hash (by setting a local hash pointer to the start/end of the array and retrieving the key for that index). By necessity, array_key_index() iterates through the keys, but does so in C code, rather than in PHP code. array_key_first() and array_key_last() are implemented in terms of array_key_index(); | ||
- | With the reference implementation provided below, | + | array_key_first() and array_key_last() |
+ | |||
+ | ===== Patches and Tests ===== | ||
+ | The GitHub Pull request for this change is here: https:// | ||
+ | |||
+ | This is a full and working implementation, and if accepted, could be merged as-is. The PR includes tests covering all new functionality. | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 54: | Line 82: | ||
===== Open Issues ===== | ===== Open Issues ===== | ||
- | None noted at this time. | + | * Whether or not to include the corresponding array value as an optional-return-by-reference parameter. |
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
Line 60: | Line 88: | ||
This RFC does not add new syntax, so a 50%+1 majority is required to pass. | This RFC does not add new syntax, so a 50%+1 majority is required to pass. | ||
- | |||
- | ===== Patches and Tests ===== | ||
- | The GitHub Pull request for this change is here: https:// | ||
- | |||
- | This is a full and working implementation, | ||
===== Implementation ===== | ===== Implementation ===== | ||
Line 71: | Line 94: | ||
- a link to the git commit(s) | - a link to the git commit(s) | ||
- a link to the PHP manual entry for the feature | - a link to the PHP manual entry for the feature | ||
- |
rfc/array_key_first_last_index.1449508042.txt.gz · Last modified: 2017/09/22 13:28 (external edit)