# [DRAFT] RFC - array_column() function

- Thread: https://externals.io/message/60930
- Original archive: https://news-web.php.net/php.internals/60930
- Started by Ben Ramsey on 2012-06-21T19:27:25+00:00
- Messages: 13
- 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

---

## #60930 — Ben Ramsey — 2012-06-21T19:27:25+00:00

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

Hi all,

I've written a draft version of an RFC for pull request 56 ( 
https://github.com/php/php-src/pull/56 ), to add `array_column()` to the 
core. Please take a look and provide feedback so that I can improve the 
patch/proposal.

https://wiki.php.net/rfc/array_column

Thanks,
Ben

---

## #60931 — Paul Dragoonis — 2012-06-21T20:14:27+00:00

URL: https://externals.io/message/60930#60931 · In reply to: #60930

> Hi all,
>
> I've written a draft version of an RFC for pull request 56 (
> https://github.com/php/php-src/pull/56 ), to add `array_column()` to the core.
> Please take a look and provide feedback so that I can improve the
> patch/proposal.
>
> https://wiki.php.net/rfc/array_column

I like your proposal and code examples on the RFC, this function is
useful and would save a foreach() to get the specified keys/indexes
out of the array.

- Paul.

>
> Thanks,
> Ben

---

## #60936 — contact@leo-peltier.fr — 2012-06-22T10:32:26+00:00

URL: https://externals.io/message/60930#60936 · In reply to: #60930

Hi,

Shouldn't this be called 'array_pluck'?
This is the name people usually use when implementing it in PHP (see 
"array_pluck(" vs "array_column(" in Google) or in other languages/libs (see 
underscorejs, prototypejs and RoR).

-- 
Léo Peltier

---

## #60937 — Ben Ramsey — 2012-06-22T15:52:09+00:00

URL: https://externals.io/message/60930#60937 · In reply to: #60936

> Hi,
>
> Shouldn't this be called 'array_pluck'?
> This is the name people usually use when implementing it in PHP (see
> "array_pluck(" vs "array_column(" in Google) or in other languages/libs
> (see underscorejs, prototypejs and RoR).
>

I'm open to changing or aliasing the name to array_pluck(), if others 
are in agreement.

-Ben

---

## #60942 — Tom Sartain — 2012-06-22T20:59:38+00:00

URL: https://externals.io/message/60930#60942 · In reply to: #60937

> 
>
> > […]
> I'm open to changing or aliasing the name to array_pluck(), if others are
> in agreement.


+1 on using array_pluck.

The term 'column' makes a lot of sense for PDO working with database
columns, but there is no concept of a 'column' in the array structure or
PHP as a whole, outside of database related functions.

- Tom Sartain

---

## #60943 — Scott MacVicar — 2012-06-22T21:20:52+00:00

URL: https://externals.io/message/60930#60943 · In reply to: #60937

Hey Ben,



> 
> > […]
> 
> I'm open to changing or aliasing the name to array_pluck(), if others are in agreement.


We have a version of this at Facebook and its called array_pull() it has a third parameter and supports pulling a new key value too. You can find a copy of it in Phabricator. There is also mpull() which is for methods. I'd recommend combining the two methods to be honest and doing a single function that supports index names or a method.

https://github.com/facebook/libphutil/blob/master/src/utils/utils.php#L143

- S

---

## #60949 — Stas Malyshev — 2012-06-23T20:43:22+00:00

URL: https://externals.io/message/60930#60949 · In reply to: #60937

Hi!

> I'm open to changing or aliasing the name to array_pluck(), if others 
> are in agreement.

I wouldn't know what "pluck" means here. "Column" is a clear word with
established meaning. Let's not get too whimsical here.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #60958 — Ralph Schindler — 2012-06-24T16:19:19+00:00

URL: https://externals.io/message/60930#60958 · In reply to: #60942

> The term 'column' makes a lot of sense for PDO working with database
> columns, but there is no concept of a 'column' in the array structure or
> PHP as a whole, outside of database related functions.

Actually, while perhaps most of the time a value from a column is what 
people retrieving from a database result set, that is not always the 
case.  The result could be the value from an expression or a field from 
a database view.  Most database extensions outside of PDO do in fact 
call it a "field", which I (too) feel is more appropriate.

In the case of this addition, I do indeed like array_pluck as well, as 
column feel like an overloaded term.

-ralph

---

## #60961 — Matthew Weier O'Phinney — 2012-06-25T14:44:02+00:00

URL: https://externals.io/message/60930#60961 · In reply to: #60949

> > […]
>
> I wouldn't know what "pluck" means here. "Column" is a clear word with
> established meaning. Let's not get too whimsical here.

Nothing whimsical about it at all, Stas. The definition is:

    Take hold of (something) and quickly remove it from its place; pick

and synonyms include "pull" and "gather".

As Ralph noted, "column" is overloaded, as it has connotations dealing
with databases as well as tables, and arrays often represent neither.

-- 
Matthew Weier O'Phinney
Project Lead            | matthew@zend.com
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

---

## #60963 — Ben Ramsey — 2012-06-25T19:22:31+00:00

URL: https://externals.io/message/60930#60963 · In reply to: #60961

> 
> > […]
>
> Nothing whimsical about it at all, Stas. The definition is:
>
>      Take hold of (something) and quickly remove it from its place; pick
>
> and synonyms include "pull" and "gather".
>
> As Ralph noted, "column" is overloaded, as it has connotations dealing
> with databases as well as tables, and arrays often represent neither.
>

I agree with Tom and Ralph that we should look at what others are doing 
and pick a similar name. After searching on "pluck," though, I'm getting 
mixed results on how pluck is used:

In Rails, it looks like pluck is used for the purpose that I've created 
array_column, but it's used with ActiveRecord as a way to pull a column 
of results from the database (much like PDOStatement::fetchColumn). See 
here:

https://github.com/rails/rails/commit/a382d60f6abc94b6a965525872f858e48abc00de

However, in Prototype.js and Underscore.js, pluck seems behave more like 
 `array_map()` in PHP:

http://api.prototypejs.org/language/Enumerable/prototype/pluck/
http://documentcloud.github.com/underscore/#pluck

Nevertheless, it would technically have the same effect as the "column" 
functionality, since calling that method/property in Javascript simply 
returns the value of the property or result of the method call.

Also, the Python community has recently discussed adding pluck for lists:

https://groups.google.com/forum/?fromgroups#!topic/python-ideas/p9qtUzg9zsk

It looks like they already have some functionality that implements 
similar behavior, though:

> > […]
{'name': 'curly', 'age': 60}]
> > […]
['moe', 'larry', 'curly']

array_column/pluck in PHP would do the same:
$names = array_column($stooges, 'name');

If other languages/frameworks/libraries are using pluck to mean exactly 
what this implementation means, then I agree with changing the name to 
array_pluck, but if "pluck" also carries meaning similar to array_map, 
then I don't want to confuse folks.

-Ben

---

## #60964 — Aaron Holmes — 2012-06-25T20:31:41+00:00

URL: https://externals.io/message/60930#60964 · In reply to: #60963

For what my .02 is worth, I imagine "plucking" an item out of an array 
would not occur to a user-land developer as the proper nomenclature for 
extracting all the values from an array with the given key. At worst, 
this means many developers simply won't learn about the function and 
continue to implement it in user-land.

When array_column was offered as the name, I immediately knew what the 
purpose was, before checking the RFC.


> 
> > […]
>
> I agree with Tom and Ralph that we should look at what others are 
> doing and pick a similar name. After searching on "pluck," though, I'm 
> getting mixed results on how pluck is used:
>
> In Rails, it looks like pluck is used for the purpose that I've 
> created array_column, but it's used with ActiveRecord as a way to pull 
> a column of results from the database (much like 
> PDOStatement::fetchColumn). See here:
>
> https://github.com/rails/rails/commit/a382d60f6abc94b6a965525872f858e48abc00de 
>
>
> However, in Prototype.js and Underscore.js, pluck seems behave more 
> like `array_map()` in PHP:
>
> http://api.prototypejs.org/language/Enumerable/prototype/pluck/
> http://documentcloud.github.com/underscore/#pluck
>
> Nevertheless, it would technically have the same effect as the 
> "column" functionality, since calling that method/property in 
> Javascript simply returns the value of the property or result of the 
> method call.
>
> Also, the Python community has recently discussed adding pluck for lists:
>
> https://groups.google.com/forum/?fromgroups#!topic/python-ideas/p9qtUzg9zsk 
>
>
> It looks like they already have some functionality that implements 
> similar behavior, though:
>
> > […]
> {'name': 'curly', 'age': 60}]
> > […]
> ['moe', 'larry', 'curly']
>
> array_column/pluck in PHP would do the same:
> $names = array_column($stooges, 'name');
>
> If other languages/frameworks/libraries are using pluck to mean 
> exactly what this implementation means, then I agree with changing the 
> name to array_pluck, but if "pluck" also carries meaning similar to 
> array_map, then I don't want to confuse folks.
>
> -Ben

---

## #60965 — Paul Dragoonis — 2012-06-26T00:35:12+00:00

URL: https://externals.io/message/60930#60965 · In reply to: #60964

What about array_extract ?



> For what my .02 is worth, I imagine "plucking" an item out of an array
> would not occur to a user-land developer as the proper nomenclature for
> extracting all the values from an array with the given key. At worst, this
> means many developers simply won't learn about the function and continue to
> implement it in user-land.
>
> When array_column was offered as the name, I immediately knew what the
> purpose was, before checking the RFC.
>
>
> 
>
> > […]

---

## #60966 — Aaron Holmes — 2012-06-26T02:59:04+00:00

URL: https://externals.io/message/60930#60966 · In reply to: #60965

The name makes sense, but would likely be confused with `extract()` 
http://us3.php.net/extract


> What about array_extract ?
>
> On Mon, Jun 25, 2012 at 9:31 PM, Aaron Holmes <aaron@aaronholmes.net 
> <mailto:aaron@aaronholmes.net>> wrote:
>
>     For what my .02 is worth, I imagine "plucking" an item out of an
>     array would not occur to a user-land developer as the proper
>     nomenclature for extracting all the values from an array with the
>     given key. At worst, this means many developers simply won't learn
>     about the function and continue to implement it in user-land.
>
>     When array_column was offered as the name, I immediately knew what
>     the purpose was, before checking the RFC.
>
>
>     
>
>         
>
>             On 2012-06-23, Stas Malyshev <smalyshev@sugarcrm.com
>             <mailto:smalyshev@sugarcrm.com>> wrote:
>
>                     I'm open to changing or aliasing the name to
>                     array_pluck(), if others
>                     are in agreement.
>
>
>                 I wouldn't know what "pluck" means here. "Column" is a
>                 clear word with
>                 established meaning. Let's not get too whimsical here.
>
>
>             Nothing whimsical about it at all, Stas. The definition is:
>
>                 Take hold of (something) and quickly remove it from
>             its place; pick
>
>             and synonyms include "pull" and "gather".
>
>             As Ralph noted, "column" is overloaded, as it has
>             connotations dealing
>             with databases as well as tables, and arrays often
>             represent neither.
>
>
>         I agree with Tom and Ralph that we should look at what others
>         are doing and pick a similar name. After searching on "pluck,"
>         though, I'm getting mixed results on how pluck is used:
>
>         In Rails, it looks like pluck is used for the purpose that
>         I've created array_column, but it's used with ActiveRecord as
>         a way to pull a column of results from the database (much like
>         PDOStatement::fetchColumn). See here:
>
>         https://github.com/rails/rails/commit/a382d60f6abc94b6a965525872f858e48abc00de
>
>
>         However, in Prototype.js and Underscore.js, pluck seems behave
>         more like `array_map()` in PHP:
>
>         http://api.prototypejs.org/language/Enumerable/prototype/pluck/
>         http://documentcloud.github.com/underscore/#pluck
>
>         Nevertheless, it would technically have the same effect as the
>         "column" functionality, since calling that method/property in
>         Javascript simply returns the value of the property or result
>         of the method call.
>
>         Also, the Python community has recently discussed adding pluck
>         for lists:
>
>         https://groups.google.com/forum/?fromgroups#!topic/python-ideas/p9qtUzg9zsk
>         <https://groups.google.com/forum/?fromgroups#%21topic/python-ideas/p9qtUzg9zsk>
>
>
>         It looks like they already have some functionality that
>         implements similar behavior, though:
>
> > […]
>         'age': 50}, {'name': 'curly', 'age': 60}]
> > […]
>         ['moe', 'larry', 'curly']
>
>         array_column/pluck in PHP would do the same:
>         $names = array_column($stooges, 'name');
>
>         If other languages/frameworks/libraries are using pluck to
>         mean exactly what this implementation means, then I agree with
>         changing the name to array_pluck, but if "pluck" also carries
>         meaning similar to array_map, then I don't want to confuse folks.
>
>         -Ben
