rfc:array_change_keys

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:array_change_keys [2016/05/29 17:53] – Clarify the two exceptions to all values being present in the output colinodellrfc:array_change_keys [2018/11/27 23:34] (current) – Withdrawing the RFC colinodell
Line 3: Line 3:
   * Date: 2016-05-29   * Date: 2016-05-29
   * Authors: Colin O'Dell <colinodell@gmail.com>, Jeremy Mikola <jmikola@gmail.com>   * Authors: Colin O'Dell <colinodell@gmail.com>, Jeremy Mikola <jmikola@gmail.com>
-  * Status: Discussion+  * Status: Withdrawn
   * First Published at: http://wiki.php.net/rfc/array_change_keys   * First Published at: http://wiki.php.net/rfc/array_change_keys
  
Line 24: Line 24:
 </code> </code>
  
-This approach requires no less than 4 lines of code.  Furthermore, this logic cannot be wrapped as a parameter to some other function.+This approach requires 4 lines of code.  Furthermore, this logic cannot be wrapped as a parameter to some other function.
  
 That latter issue can solved by composing several existing functions like so: That latter issue can solved by composing several existing functions like so:
Line 31: Line 31:
 <?php <?php
 $newArray = array_combine( $newArray = array_combine(
-    array_map(function ($value) { +    array_map( 
-        return 'someValue'; // Whatever custom logic is needed +        function ($key, $value) { 
-    }, array_values($oldArray)), +            return 'someValue'; // Whatever custom logic is needed 
-    array_values($oldArray)+        }, 
 +        array_keys($oldArray)
 +        $oldArray 
 +    ), 
 +    $oldArray
 ); );
 </code> </code>
Line 40: Line 44:
 While this does work, it has some major drawbacks: While this does work, it has some major drawbacks:
  
-  * Requires no less than 4 function calls, each of which returns a new array. +  * The code is convoluted - its purpose is not immediately obvious. 
-  * The anonymous function only has access to the original key **or** the original value - **not both**+  * Requires at least 3 separate function calls, each of which returns a new array. 
-  The purpose of the code is not immediately obvious.+  * The input array is referenced 3 times
 +      If you want to re-key an iterator's results, you cannot do so inline - a temporary variable would be needed to convert the iterator to array first. 
  
 ===== Proposal ===== ===== Proposal =====
 This RFC proposes the creation of a new core function ''array_change_keys()'': This RFC proposes the creation of a new core function ''array_change_keys()'':
 +
 +<code php>
 +$newArray = array_change_keys($originalArray, function ($key, $value) {
 +    return 'someValue'; // Whatever custom logic is needed
 +});
 +</code>
 +
 +Function definition:
  
 <code php> <code php>
Line 99: Line 113:
 ==== Function Name ==== ==== Function Name ====
  
-PHP already has an ''array_change_key_case()'' function, so sharing a common root name (''array_change_key'') seems like a logic choice.+PHP already has an ''array_change_key_case()'' function, so sharing a common root name (''array_change_key'') seems like a logical choice.
  
-Other function which deal with multiple keys (like ''array_keys'' and ''array_fill_keys'') are pluralized, so we're using that same convention here.+Other functions which deal with multiple keys (like ''array_keys'' and ''array_fill_keys'') are pluralized, so we're using that same convention here.
  
 ===== Discussion ===== ===== Discussion =====
Line 109: Line 123:
 ==== Pros ==== ==== Pros ====
  
-=== Faster Execution ===+=== Provides Common General-Purpose Functionality ===
  
-Re-keying an array with ''array_change_keys()'' is faster than the two alternate approaches shown earlier:+PHP already has an ''array_change_key_case'' function, which is an incredibly specific implementation that isn't useful in the majority of cases where an array needs to be re-keyed.  By providing a general-purpose function for a common problem we prevent the need for other ''array_change_key_*'' variants in the future.
  
-{{:rfc:array_change_keys_benchmark.png?800|}}+=== Usefulness === 
 + 
 +Needing to re-key array is a common task for some PHP developers, especially those needing their array to work like a dictionary.
  
 === Cleaner Code === === Cleaner Code ===
  
 Using this function makes it immediately obvious to other developers that an array is being re-keyed. Using this function makes it immediately obvious to other developers that an array is being re-keyed.
 +
 +=== Matches Existing Behavior ===
 +
 +The "edge cases" mentioned above (returning invalid types or duplicate keys) matches existing behavior in PHP that developers already understand and expect.  No new edge cases or quirks are being introduced with this RFC.
 +
 +=== Faster Execution Than array_combine() ===
 +
 +Re-keying an array with ''array_change_keys()'' is faster than the ''array_combine'' approach:
 +
 +{{:rfc:array_change_keys_bench1.png?600|}}
 +
 +{{:rfc:array_change_keys_bench2.png?600|}}
 +
 +(Benchmarks generated with https://github.com/Ocramius/array_change_keys-benchmark)
 +
 +=== Works With Functional Code ===
 +
 +This function can be nested inside of other method calls for function composition.  The same is not possible for the ''foreach'' approach (without requiring the creation of a separate method to encapsulate that functionality).
  
 ==== Cons ==== ==== Cons ====
 +
 +=== Slower Than foreach ===
 +
 +As noted in the benchmarks above, the ''foreach'' loop approach is faster than ''array_change_keys'' in most (but not all) cases.
 +
 +=== Does Not Support Traversable ===
 +
 +Like the other ''array_'' functions, this one also doesn't support iterators, which may be seen as a step backwards.
 +
 +=== Easily Implemented In User Land ===
 +
 +This function can be implemented in user land using one of the alternative approaches shown above.  There's a general feeling among some developers that "what can be implemented in userland shouldn't be in core".
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 142: Line 188:
 ===== Future Scope ===== ===== Future Scope =====
  
-Returning ''__toString()''able objects from the callback is not supported by this RFC.  This matches the behavior of other functions like ''array_flip()'' and ''array_fill_keys()'' If such functionality is desired, a separate RFC could be created to add this functionality to all similar functions.+Other ''array_'' functions do not support certain features like using ''__toString()'' for keys or supporting ''Traversable'' objects like iterators, so support for them is not being proposed here either.  If such functionality is desired, a separate RFC could be created to add this functionality to all similar functions.
  
-===== Proposed Voting Choices =====+===== Voting =====
 Because this is not a language change, a 50%+1 vote will be required to add this new function. Because this is not a language change, a 50%+1 vote will be required to add this new function.
  
Line 154: Line 200:
 ===== References ===== ===== References =====
 Links to external references, discussions or RFCs Links to external references, discussions or RFCs
 +
 +Mailing list discussion: [[https://marc.info/?l=php-internals&m=146452769326964&w=2]]
 +
 +Reddit discussion: [[https://www.reddit.com/r/PHP/comments/4ll1hg/rfc_array_change_keys/]]
rfc/array_change_keys.1464544386.txt.gz · Last modified: 2017/09/22 13:28 (external edit)