On 23/04/2022 23:42, mickmackusa wrote:
Ideally, PHP should have a native transposing function to put
developers out of their misery -- much like array_key_last() did.


I think a better comparison would be array_column - https://wiki.php.net/rfc/array_column One interesting thing that Ben Ramsey wrote alongside that RFC was a pure PHP implementation with identical behaviour, available for use as a polyfill: https://github.com/ramsey/array_column

Unlike array_column, I don't recall personally needing an array_transpose function, but am willing to believe that many people do.


My first piece of advice would be to start with a definition of what you mean by "transpose" in this context, with an example of input and output in the basic case, and then a few scenarios where it's useful.

Having that clear would then help understand the details you go into, each of which could be accompanied by its own example. For instance:


Let's create an intuitive transposing function for data sets with a
minimum depth of 2 levels.


I can't picture what "transpose" means for something with more than 2 levels.


As a matter of flexibility, there should be no requirement that the
lone parameter be a matrix or dense data structure.


I have no idea what this means.


It should also preserve keys to give it a clear advantage over
pre-existing functional techniques.


Preserve what keys?


     // [['single' => 'row']] becomes ['single' => 'row'] but should be
['single' => ['row']]


Should it? Why?


My second piece of advice is to remember that flexibility, ease of use, and efficiency, are three corners of a triangle, and you can't optimise for all three. array_column does a reasonable job of covering multiple use cases, but there have been several unsuccessful requests over the years to enhance it or create variations for other use cases, so you're never going to make everyone happy.


Should the function unconditionally return an array of arrays?
Should it preserve the initial data types of the first and second level?
Should data type preservation be excluded to increase the chances of
its implementation?
[...]
     1. Unconditionally cast output as array of arrays.
[...]
     2. Preserve original data types of level one and level two.
https://3v4l.org/RRKlr

(If input is two dimensional, then the first occurring data type in
the second level will dictate the result's first level data type.)
[...]
If only initially implemented to return an array of arrays, then
expanding the result data types in the future may be of interest.


Without really understanding what you're saying, my suspicion is that these paragraphs are trying too hard to make a Swiss-army knife rather than a sharp blade.


List of Stack Overflow pages which seek to implement transposition


Would all of these requirements be satisfied by the same implementation, without needing a complex set of options? What can we learn from the links about what features are likely to be most useful to people?


I look forward to seeing a draft RFC, which can take the time to explain the features you think are needed.


Regards,

--
Rowan Tommins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to