rfc:array_key_first_last_index

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Last revisionBoth sides next revision
rfc:array_key_first_last_index [2016/01/01 20:43] jbaffordrfc:array_key_first_last_index [2016/01/04 07:17] – updated introduction; add reference to end(); s/current/key/ jbafford
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: 2016-01-01   * Date: 2016-01-01
   * Author: John Bafford   * Author: John Bafford
Line 8: Line 8:
  
 ===== 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 to assist with that task:
  
   * $key = array_key_first($array [, &$value])   * $key = array_key_first($array [, &$value])
Line 59: Line 59:
       * foreach($arr as $key => $value); //Iterate over the entire array, doing nothing, just for the side-effect of $key still contains the last array key beyond the end of the loop.//       * foreach($arr as $key => $value); //Iterate over the entire array, doing nothing, just for the side-effect of $key still contains the last array key beyond the end of the loop.//
  
-  * reset($arr); $key = current($arr);+  * reset($arr); $key = key($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(); array_key_index() exists a a consequence and extension of generalizing the common code between array_key_first() and array_key_last().
-With the reference implementation provided 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(); array_key_index() exists a a consequence and extension of generalizing the common code between array_key_first() and array_key_last().+
  
 array_key_first() and array_key_last() have O(1) time complexity. array_key_index() (when not retrieving the first or last key) has O(N) time complexity. array_key_index() is intended when only one key or value from an array is needed; if repeated calls to array_key_index() are needed for a particular array, it would likely be more performant to just use array_keys() instead. (Iterating all keys of an array using array_key_index() would be O(N^2).) As such, it's included for sake of completeness, but would likely in practice see limited use. array_key_first() and array_key_last() have O(1) time complexity. array_key_index() (when not retrieving the first or last key) has O(N) time complexity. array_key_index() is intended when only one key or value from an array is needed; if repeated calls to array_key_index() are needed for a particular array, it would likely be more performant to just use array_keys() instead. (Iterating all keys of an array using array_key_index() would be O(N^2).) As such, it's included for sake of completeness, but would likely in practice see limited use.
Line 83: 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 =====
rfc/array_key_first_last_index.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1