rfc:array_change_keys
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:array_change_keys [2016/05/29 11:48] – colinodell | rfc:array_change_keys [2016/05/31 01:32] – Update pros & cons colinodell | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Your Title Here ====== | + | ====== PHP RFC: array_change_keys() |
* Version: 0.1 | * Version: 0.1 | ||
- | * Date: 2016-05-28 | + | * Date: 2016-05-29 |
- | * Authors: Colin O'Dell < | + | * Authors: Colin O'Dell < |
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
Line 24: | Line 24: | ||
</ | </ | ||
- | This approach requires | + | This approach requires 4 lines of code. Furthermore, |
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 ' | + | |
- | }, array_values($oldArray)), | + | return ' |
- | | + | }, |
+ | array_keys($oldArray), | ||
+ | $oldArray | ||
+ | | ||
+ | $oldArray | ||
); | ); | ||
</ | </ | ||
Line 40: | Line 44: | ||
While this does work, it has some major drawbacks: | While this does work, it has some major drawbacks: | ||
- | * Requires | + | |
- | * The anonymous function only has access to the original key **or** the original value - **not both**. | + | |
- | * The purpose of the code is not immediately obvious. | + | * The input array is referenced 3 times. |
+ | * If you want to re-key an iterator' | ||
===== Proposal ===== | ===== Proposal ===== | ||
This RFC proposes the creation of a new core function '' | This RFC proposes the creation of a new core function '' | ||
+ | |||
+ | <code php> | ||
+ | $newArray = array_change_keys($originalArray, | ||
+ | return ' | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | Function definition: | ||
<code php> | <code php> | ||
Line 56: | Line 70: | ||
- A '' | - A '' | ||
- | The new '' | + | (This parameter order is consistent with all other array functions except for '' |
- | This parameter | + | A new '' |
- | ==== Callable | + | ==== Callback |
Two parameters will be passed to the '' | Two parameters will be passed to the '' | ||
Line 71: | Line 85: | ||
=== Returning Invalid Types === | === Returning Invalid Types === | ||
- | Returning any type besides '' | + | The callable must return a valid key. |
< | < | ||
Line 83: | Line 97: | ||
=== Returning Duplicate Keys === | === Returning Duplicate Keys === | ||
- | If the callable returns the same key for multiple values, the last one " | + | If the callable returns the same key for multiple values, the last occurrence |
<code php> | <code php> | ||
Line 94: | Line 108: | ||
// } | // } | ||
</ | </ | ||
+ | |||
+ | This behavior also matches '' | ||
==== Function Name ==== | ==== Function Name ==== | ||
Line 104: | Line 120: | ||
This section will be updated with any additional pros/cons that arise during the discussion period. | This section will be updated with any additional pros/cons that arise during the discussion period. | ||
+ | |||
+ | ==== Pros ==== | ||
+ | |||
+ | === Provides Common General-Purpose Functionality === | ||
+ | |||
+ | PHP already has an '' | ||
+ | |||
+ | === 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 === | ||
+ | |||
+ | Using this function makes it immediately obvious to other developers that an array is being re-keyed. | ||
+ | |||
+ | === Faster Execution Than array_combine() === | ||
+ | |||
+ | Re-keying an array with '' | ||
+ | |||
+ | {{: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | (Benchmarks generated with https:// | ||
+ | |||
+ | === Works With Functional Code === | ||
+ | |||
+ | This function can be nested inside of other method calls for function composition. | ||
+ | |||
+ | ==== Cons ==== | ||
+ | |||
+ | === Slower Than foreach === | ||
+ | |||
+ | As noted in the benchmarks above, the '' | ||
+ | |||
+ | === Does Not Support Traversable === | ||
+ | |||
+ | Like the other '' | ||
+ | |||
+ | === Easily Implemented In User Land === | ||
+ | |||
+ | This function can be implemented in user land using one of the alternative approaches shown above. | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 119: | Line 177: | ||
==== To Opcache ==== | ==== To Opcache ==== | ||
- | **TODO: UNKNOWN** | + | Unknown |
===== Open Issues ===== | ===== Open Issues ===== | ||
- | Make sure there are no open issues when the vote starts! | + | None |
===== Future Scope ===== | ===== Future Scope ===== | ||
Line 129: | Line 187: | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | This RFC requires | + | Because this is not a language change, a 50%+1 vote will be required to add this new function. |
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | A proposed implementation is provided with this RFC. While it seems to work well, a closer review by an experienced core developer would be greatly appreciated. | + | A proposed implementation is provided with this RFC: [[https:// |
- | + | ||
- | **WIP**: https:// | + | |
- | + | ||
- | **TODO LINK TO THE FINAL PATCH HERE** | + | |
Several tests are also included. | Several tests are also included. | ||
- | |||
- | ===== Implementation ===== | ||
- | Pending. | ||
===== References ===== | ===== References ===== | ||
Links to external references, discussions or RFCs | Links to external references, discussions or RFCs |
rfc/array_change_keys.txt · Last modified: 2018/11/27 23:34 by colinodell