On Sun, Apr 24, 2022 at 10:24 PM Rowan Tommins <rowan.coll...@gmail.com> 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: > 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] >
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. With: 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 technique. 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 not swapped. 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 transposition? 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 the RFC. I don't program in C so I don't know if there are any optimizations that can be enjoyed. Indicated by Stack Overflow pages that I've found, array_transpose() would be useful when re-orientating $_POST, $_FILES, csv data, curl response data, or sql result set data. As a simple example, "zip" three arrays together to form an indexed array of associative arrays. https://3v4l.org/jOQte $names = ['Anne', 'Burt', 'Chad', 'Dawn']; $ages = [18, 22, 20, 21]; $pets = ['dog', 'cat', 'fish', 'bird']; var_export(array_transpose(compact(['names', 'ages', 'pets']))); Test cases comparing three transposition techniques: https://3v4l.org/Ns8M7 Given an empty array, the input array should be returned, "splat" breaks. https://3v4l.org/MZv9c Given an empty object, "basic" will return an empty array, "types" will return the input object, "splat" breaks. https://3v4l.org/0h8kc Given a 2d array with a single row, a 2d array should be returned, "splat" undesirably flattens to a 1d array. https://3v4l.org/8dnKr Given an assoc (gapped/non-dense, not a matrix) array of indexed arrays, an indexed array of associative arrays should be returned, "splat" breaks. https://3v4l.org/nob75 Given an indexed (matrix) array of assoc arrays, an assoc array of indexed arrays should be returned, "splat" does not preserve assoc first level keys. https://3v4l.org/2lYYAj Given an indexed array of objects (has more than 2 levels), "basic" returns an array of arrays, "types" returns an object of arrays, "splat" breaks. https://3v4l.org/peqOO Given an object containing an object and an array, "basic" returns an array of arrays, "types" returns an object or objects, "splat" breaks. https://3v4l.org/Eh83G Given an array containing two empty arrays, all return an empty array. https://3v4l.org/IPXqr Given an array containing two empty arrays, then a single-element array, "basic" and "types" remove the empty arrays and transpose the third array, "splat" fills empty array position with nulls. https://3v4l.org/EE39i Given an array containing two arrays containing an array each, all return an array containing an array containing two arrays. mickmackusa -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php