[VOTE] array_part()

php.internals

Gustavo Lopes

14 years ago
I've opened the array_part() vote: https://wiki.php.net/rfc/array_part#vote The vote asks whether the branch at https://github.com/cataphract/php-src/tree/array_part should be merged. This implementation has been tested and has 100% code coverage on the reachable code.
-- Gustavo Lopes

Stas Malyshev

14 years ago
Hi!
> I've opened the array_part() vote: > > https://wiki.php.net/rfc/array_part#vote > > The vote asks whether the branch at > https://github.com/cataphract/php-src/tree/array_part should be merged. > This implementation has been tested and has 100% code coverage on the > reachable code.
This looks very complicated. It might be that some use cases require this level of complexity, but I can't remember a case where I would need it. That alone does not disqualify the proposal, but I personally very much like having something like array_column() rather than whole array slicing engine with it's own declarative language inside. The engine might be fine, but I imagine many users would want just something like array_column() - so at least it'd be nice to have both.
-- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/ (408)454-6900 ext. 227

Gustavo Lopes

14 years ago
On Tue, 29 May 2012 02:28:05 +0200, Stas Malyshev <smalyshev@sugarcrm.com> wrote:
> This looks very complicated. It might be that some use cases require > this level of complexity, but I can't remember a case where I would need > it. That alone does not disqualify the proposal, but I personally very > much like having something like array_column() rather than whole array > slicing engine with it's own declarative language inside. The engine > might be fine, but I imagine many users would want just something like > array_column() - so at least it'd be nice to have both.
1) Independently for whether we want array_column this function does things that no other function built-in currently does and some that no userland function can even do: * Extract slices without side effects (array_slice() has side effects). * Extract slices based on keys on time proportional to the size of the slice and not to the size of the array. * Pick an arbitrary number of elements from an array in any permutation and traversing the array, in worst case -- if we request an element in the middle --, only once. 2) A lot of people seem to think this is too complicated. I think that objection comes from several places: * People focusing on the most complex usages of the function. The test cases have to cover complex usages in order to exercise the implementation correctly, but that doesn't mean the majority of use cases will be that complex. * The syntax for contiguous slices is verbose (e.g. ['start'=>1, 'end'=>3] instead of [1:3] or [1..3] like in other languages). * Specifying we want all the elements in a level could be simpler (e.g. using null instead of ['start'=>0] or ['start'=>null]). See above. * The description of the function aims to be precise and is therefore verbose. This was probably a strategic miscalculation on my part. Let me exemplify how this function may make things simpler. Right now, let's suppose you want to drop the first element on each level. Let's say you have this array: $arr = [ ['col1', 'col2', 'col3'], ['line1', 0, 1 ], ['line2', 1, 0 ], ] To drop the headers, you'd now have to do: array_slice(array_map(function($a) { return array_slice($a,1); }, $arr), 1); With array_part, you can do: array_part($arr, [ ['start'=>1], ['start'=>1] ]); or in this case: array_part($arr, [ [1, 2], [1, 2] ]);
-- Gustavo Lopes