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 revisionBoth sides next revision
rfc:array_change_keys [2016/05/31 01:15] – Update benchmarks colinodellrfc:array_change_keys [2016/05/31 01:32] – Update pros & cons colinodell
Line 122: Line 122:
  
 ==== Pros ==== ==== Pros ====
 +
 +=== Provides Common General-Purpose Functionality ===
 +
 +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.
 +
 +=== 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() === === Faster Execution Than array_combine() ===
Line 130: Line 142:
  
 {{:rfc:array_change_keys_bench2.png?600|}} {{:rfc:array_change_keys_bench2.png?600|}}
- 
-However, it should be noted that the non-functional ''foreach'' approach is indeed faster in most (but not all) cases. 
  
 (Benchmarks generated with https://github.com/Ocramius/array_change_keys-benchmark) (Benchmarks generated with https://github.com/Ocramius/array_change_keys-benchmark)
  
-=== Cleaner Code ===+=== Works With Functional Code ===
  
-Using this function makes it immediately obvious to other developers that an array is being re-keyed.+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 =====
rfc/array_change_keys.txt · Last modified: 2018/11/27 23:34 by colinodell