[RFC] [DISCUSSION] array_part()

php.internals

Gustavo Lopes

14 years ago
Hi I have a proposal to add a new function to ext/standard: array_part(): https://wiki.php.net/rfc/array_part Comments would be very welcome, especially the constructive kind. Please keep this on topic. In particular, avoid proposing new syntax as I'm not interested in that. You can of course create your own RFC for that purpose.
-- Gustavo Lopes

Paul Dragoonis

14 years ago
Can you please add some code examples on the RFC, that get outputted as <pre> tags. Ideally demonstrating var_dump() output to inspect the return value of your new function to get a clear indication of the input, and resulting output. - Paul. On Mon, May 14, 2012 at 11:21 AM, Gustavo Lopes <glopes@nebm.ist.utl.pt>wrote:

Gustavo Lopes

14 years ago
On Mon, 14 May 2012 12:26:11 +0200, Paul Dragoonis <dragoonis@gmail.com> wrote:
> Can you please add some code examples on the RFC, that get outputted as > <pre> tags. > > Ideally demonstrating var_dump() output to inspect the return value of > your > new function to get a clear indication of the input, and resulting > output. >
The examples were linked from the RFC page. I've made the link more obvious. In any case, here is the URL: https://gist.github.com/2660601#file_test.php
-- Gustavo Lopes

Paul Dragoonis

14 years ago
Hey, Am i correct in assuming this is basically substr() for arrays. On Mon, May 14, 2012 at 11:29 AM, Gustavo Lopes <glopes@nebm.ist.utl.pt>wrote:

Gustavo Lopes

14 years ago
On Mon, 14 May 2012 13:23:27 +0200, Paul Dragoonis <dragoonis@gmail.com> wrote:
> Am i correct in assuming this is basically substr() for arrays.
No. That's a part of it. It also does indexes as keys (like array_slice) and multidimensional arrays, none of which have anything analogous in strings.
-- Gustavo Lopes

Pierre Joye

14 years ago
hi Gustavo, I would add some examples inline in the RFC, with explanation, to ease the comprehension of this new function and its possibility. The current RFC is somehow hard to digest :) Cheers, On Mon, May 14, 2012 at 1:39 PM, Gustavo Lopes <glopes@nebm.ist.utl.pt> wrote:
> On Mon, 14 May 2012 13:23:27 +0200, Paul Dragoonis <dragoonis@gmail.com> > wrote: > >> Am i correct in assuming this is basically substr() for arrays. > > > No. That's a part of it. It also does indexes as keys (like array_slice) and > multidimensional arrays, none of which have anything analogous in strings. > > > -- > Gustavo Lopes > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >
-- Pierre @pierrejoye | http://blog.thepimp.net | http://www.libgd.org

Paul Dragoonis

14 years ago
On Mon, May 14, 2012 at 2:01 PM, Pierre Joye <pierre.php@gmail.com> wrote:
> hi Gustavo, > > I would add some examples inline in the RFC, with explanation, to ease > the comprehension of this new function and its possibility. The > current RFC is somehow hard to digest :) >
Yes, that's what I was meaning earlier but was on my mobile phone. Thanks Pierre. Gustavo, why would I use array_part() if I could use array_slice() to get the parts of an array between two offsets.

Gustavo Lopes

14 years ago
On Mon, 14 May 2012 15:41:25 +0200, Paul Dragoonis <dragoonis@gmail.com> wrote:
> Gustavo, why would I use array_part() if I could use array_slice() to get > the parts of an array between two offsets. >
Obviously, if you want to do something that array_slice() already does, then you wouldn't have a lot of pressing reasons to use this instead. But this is not a very interesting question. On the other hand, if you're asking what are differences between this and array_slice(), then: * array_slice() only does slicing at the level 1 (it has no multidimensional slicing that would allow doing something like array_column(), see https://github.com/php/php-src/pull/56 ) * array_slice() only operates in an 'index-as-offset' mode, which is less efficient than 'index-as-key'. array_part() allows you to choose: $arr = [-1 => 'a', 0 => 'b']; array_slice($arr, 0, 1) // 'a' array_slice($arr, -1, 1) // 'b' array_part($arr, [0]) // 'a' array_part($arr, [-1]) // 'b' array_part($arr, [-1], true) // 'a' * array_slice() has side-effects; it resets the internal array pointer. * array_slice() can preserve keys, array_part() cannot because when collapsing levels this would mean the possibility of overwriting elements. I'll improve the RFC with inline examples and this information later.
-- Gustavo Lopes