On Sun, Apr 24, 2022 at 10:24 PM Rowan Tommins wrote:
> 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:
> 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
> > 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.
> Rowan Tommins
Thanks for the feedback, Rowan. Here are some answers and thoughts...
The basic premise of transposition is to replace the first level keys
with the second level keys.
This can be visualized as converting columns of data into rows of data.
A B C
D E F
G H I
the expected result is:
A D G
B E H
C F I
When dealing with an indexed array of indexed arrays with more than
one row, `array_map(null, ...$array)` is a beautiful, concise
However, an array with a single row returns an unexpected structure,
and associative arrays and objects cause trouble.
Using nested foreach loops is reliable, but is less elegant, requires
multiple temporary variables, certainly more verbose, and must be
wrapped in a custom function for functional-style coding.
Like array_column()'s ability to respect object data, I felt it might
be beneficial to allow/respect objects with this function proposal.
I've made a judgment call to swap the data types of the first level
with the second level, but perhaps data types should be honored, yet
With my type-preserving implementation, an array of objects will
become an object of arrays.
Perhaps an array of objects should remain an array of objects after
I'm open to feedback on this behavior.
If there is no support for any type preservation (swiss army knife)
and the function should unconditionally return an array of arrays,
then it will improve performance to use the "basic" implementation for
I don't program in C so I don't know if there are any optimizations
that can be enjoyed.