# [RFC] [DISCUSSION] array_part()

- Thread: https://externals.io/message/60568
- Original archive: https://news-web.php.net/php.internals/60568
- Started by Gustavo Lopes on 2012-05-14T10:21:27+00:00
- Messages: 9
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #60568 — Gustavo Lopes — 2012-05-14T10:21:27+00:00

URL: https://externals.io/message/60568#60568

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

---

## #60569 — Paul Dragoonis — 2012-05-14T10:26:11+00:00

URL: https://externals.io/message/60568#60569 · In reply to: #60568

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:

> Hi
>
> I have a proposal to add a new function to ext/standard: array_part():
>
> https://wiki.php.net/rfc/**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

---

## #60570 — Gustavo Lopes — 2012-05-14T10:29:16+00:00

URL: https://externals.io/message/60568#60570 · In reply to: #60569

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

---

## #60573 — Paul Dragoonis — 2012-05-14T11:23:27+00:00

URL: https://externals.io/message/60568#60573 · In reply to: #60570

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:

> 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
> > […]
> 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<https://gist.github.com/2660601#file_test.php>
>
>
> --
> Gustavo Lopes

---

## #60575 — Gustavo Lopes — 2012-05-14T11:39:28+00:00

URL: https://externals.io/message/60568#60575 · In reply to: #60573

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

---

## #60577 — Pierre Joye — 2012-05-14T13:01:29+00:00

URL: https://externals.io/message/60568#60577 · In reply to: #60575

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, 14 May 2012 13:23:27 +0200, Paul Dragoonis <dragoonis@gmail.com>
> wrote:
>
> > […]
>
>
> 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

@pierrejoye | http://blog.thepimp.net | http://www.libgd.org

---

## #60579 — Paul Dragoonis — 2012-05-14T13:41:25+00:00

URL: https://externals.io/message/60568#60579 · In reply to: #60577

> 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.


>
>
> Cheers,
>
> On Mon, May 14, 2012 at 1:39 PM, Gustavo Lopes <glopes@nebm.ist.utl.pt>
> wrote:
> > […]
> and
> > […]
> strings.
> > […]
>
>
>
> --
> Pierre
>
> @pierrejoye | http://blog.thepimp.net | http://www.libgd.org

---

## #60580 — Gustavo Lopes — 2012-05-14T14:03:21+00:00

URL: https://externals.io/message/60568#60580 · In reply to: #60579

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

---

## #60639 — Gustavo Lopes — 2012-05-21T21:14:05+00:00

URL: https://externals.io/message/60568#60639

First, I'd like to announce the native implementation is available on  
github:

https://github.com/cataphract/php-src/compare/array_part

Please review this feature BEFORE voting starts. Unless something comes up  
that has to be dealt with until then, I want to start voting on Monday,  
28th May.

Now my answer to Richard:



> 
> > […]
>
> A predictable LIFO or FIFO for any collapsed keys would be reasonable,
> and probably not much more work to implement.
>
> By LIFO I mean that the last value to be put under a key would "win" I
> think that would be the most natural.
>

Such a behavior would be possible and would not be difficult to implement.  
However, it raises questions that are not easy to answer like:
- should it keep the first or the last keys? Or perhaps it could instead  
collect all the elements under a new array.
- should the behavior for numeric keys differ from that of string keys  
(perhaps in a array_merge way?)

Since the path to add this behavior in the future is straightforward (add  
another optional argument, like in array_slice), not including it causes  
little damage in terms of future growth.

On the other hand, my hope is that limiting the current design decisions  
to a easy subset may allow this to be merged rapidly.

-- 
Gustavo Lopes
