Welcome! Log In Create A New Profile

Advanced

[PHP-DEV] [DRAFT] RFC - array_column() function

Posted by Ben Ramsey 
Ben Ramsey
[PHP-DEV] [DRAFT] RFC - array_column() function
June 21, 2012 09:30PM
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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Paul Dragoonis
Re: [PHP-DEV] [DRAFT] RFC - array_column() function
June 21, 2012 10:20PM
On Thu, Jun 21, 2012 at 8:27 PM, Ben Ramsey <[email protected]> wrote:
> 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
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Léo Peltier
Re: [PHP-DEV] [DRAFT] RFC - array_column() function
June 22, 2012 12:40PM
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



--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Ben Ramsey
Re: [PHP-DEV] [DRAFT] RFC - array_column() function
June 22, 2012 06:00PM
On 6/22/12 5:32 AM, Léo Peltier wrote:
> 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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Anatoliy Belsky
Re: [PHP-DEV] [DRAFT] RFC - array_column() function
June 22, 2012 09:40PM
Hi Ben,

I've tested your patch on Windows, it compiles and the tests pass. All the
best with that :)

Regards

Anatoliy

Am Fr, 22.06.2012, 17:52 schrieb Ben Ramsey:
> On 6/22/12 5:32 AM, Léo Peltier wrote:
>> 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
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Tom Sartain
Re: [PHP-DEV] [DRAFT] RFC - array_column() function
June 22, 2012 11:10PM
On Fri, Jun 22, 2012 at 11:52 AM, Ben Ramsey <[email protected]> wrote:

> On 6/22/12 5:32 AM, Léo Peltier wrote:
>
>> 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.


+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
Scott MacVicar
Re: [PHP-DEV] [DRAFT] RFC - array_column() function
June 22, 2012 11:30PM
Hey Ben,

On 22 Jun 2012, at 08:52, Ben Ramsey wrote:

> On 6/22/12 5:32 AM, Léo Peltier wrote:
>> 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.


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
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Larry Garfield
Re: [PHP-DEV] [DRAFT] RFC - array_column() function
June 23, 2012 07:20AM
On 06/22/2012 04:20 PM, Scott MacVicar wrote:
> Hey Ben,
>
> On 22 Jun 2012, at 08:52, Ben Ramsey wrote:
>
>> On 6/22/12 5:32 AM, Léo Peltier wrote:
>>> 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.
>
> 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

+1 on this functionality from me. I've had to implement it in
user-space way too often. :-) For the name, I'd suggest doing a survey
of other languages that PHP folks are likely to have had experience in
and going with whatever seems most common/similar to what's proposed
here, rather than debating it directly. :-)

--Larry Garfield

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Stas Malyshev
Re: [PHP-DEV] [DRAFT] RFC - array_column() function
June 23, 2012 10:50PM
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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Ralph Schindler
Re: [PHP-DEV] [DRAFT] RFC - array_column() function
June 24, 2012 06:30PM
> 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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Morgan L. Owens
Re: Re: [PHP-DEV] [DRAFT] RFC - array_column() function
June 25, 2012 04:50PM
On 2012-06-25 04:19, Ralph Schindler wrote:
>> 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.
>
> In the case of this addition, I do indeed like array_pluck as well, as
> column feel like an overloaded term.
>
While casting around for other array_* names, I remembered that
array_keys() can take a second (and third) argument. With it, the
function returns all keys indexing a given value.

What we're discussing here is basically a dual to that: return all
values indexed by a given key.[*]

If the duality is continued, then what we get is

array_values(array $input[, mixed $key])

(A third $strict argument would probably do more harm than good: the key
can only be an integer or a non-numeric string, and using the same type
coercion rules would smooth out differences in the $key argument.)

[*] Yes, I know the symmetry isn't perfect, and why; the question is
whether the difference is more likely to result in "Well, duh!" or "WTF?!".

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Matthew Weier O'Phinney
Re: [PHP-DEV] [DRAFT] RFC - array_column() function
June 25, 2012 04:50PM
On 2012-06-23, Stas Malyshev <[email protected]> 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.

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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Ben Ramsey
Re: [PHP-DEV] [DRAFT] RFC - array_column() function
June 25, 2012 09:30PM
On 6/25/12 9:44 AM, Matthew Weier O'Phinney wrote:
> On 2012-06-23, Stas Malyshev <[email protected]> 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

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

>>> stooges=[{'name': 'moe', 'age': 40}, {'name': 'larry', 'age': 50},
{'name': 'curly', 'age': 60}]
>>> names=[guy['name'] for guy in stooges]
>>> print names
['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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Aaron Holmes
Re: [PHP-DEV] [DRAFT] RFC - array_column() function
June 25, 2012 10:40PM
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 6/25/2012 12:22 PM, Ben Ramsey wrote:
> On 6/25/12 9:44 AM, Matthew Weier O'Phinney wrote:
>> On 2012-06-23, Stas Malyshev <[email protected]> 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
>
>
> It looks like they already have some functionality that implements
> similar behavior, though:
>
> >>> stooges=[{'name': 'moe', 'age': 40}, {'name': 'larry', 'age': 50},
> {'name': 'curly', 'age': 60}]
> >>> names=[guy['name'] for guy in stooges]
> >>> print names
> ['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
>


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Paul Dragoonis
Re: [PHP-DEV] [DRAFT] RFC - array_column() function
June 26, 2012 02:40AM
What about array_extract ?

On Mon, Jun 25, 2012 at 9:31 PM, Aaron Holmes <[email protected]> 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 6/25/2012 12:22 PM, Ben Ramsey wrote:
>
>> On 6/25/12 9:44 AM, Matthew Weier O'Phinney wrote:
>>
>>> On 2012-06-23, Stas Malyshev <[email protected]> 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/**a382d60f6abc94b6a965525872f858*
>> *e48abc00dehttps://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://api.prototypejs.org/language/Enumerable/prototype/pluck/
>> http://documentcloud.github.**com/underscore/#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#!topic/python-ideas/p9qtUzg9zsk>;
>>
>> It looks like they already have some functionality that implements
>> similar behavior, though:
>>
>> >>> stooges=[{'name': 'moe', 'age': 40}, {'name': 'larry', 'age': 50},
>> {'name': 'curly', 'age': 60}]
>> >>> names=[guy['name'] for guy in stooges]
>> >>> print names
>> ['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
>>
>>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Aaron Holmes
Re: [PHP-DEV] [DRAFT] RFC - array_column() function
June 26, 2012 05:00AM
The name makes sense, but would likely be confused with extract()
http://us3.php.net/extract

On 6/25/2012 5:35 PM, Paul Dragoonis wrote:
> What about array_extract ?
>
> On Mon, Jun 25, 2012 at 9:31 PM, Aaron Holmes <[email protected]
> <mailto:[email protected]>> 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 6/25/2012 12:22 PM, Ben Ramsey wrote:
>
> On 6/25/12 9:44 AM, Matthew Weier O'Phinney wrote:
>
> On 2012-06-23, Stas Malyshev <[email protected]
> <mailto:[email protected]>> 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:
>
> >>> stooges=[{'name': 'moe', 'age': 40}, {'name': 'larry',
> 'age': 50}, {'name': 'curly', 'age': 60}]
> >>> names=[guy['name'] for guy in stooges]
> >>> print names
> ['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
>
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
> -----Original Message-----
> From: Morgan L. Owens [mailto:[email protected]]
> Sent: 25 June 2012 15:41
>
> On 2012-06-25 04:19, Ralph Schindler wrote:
> >> 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.
> >
> > In the case of this addition, I do indeed like array_pluck as
> well, as
> > column feel like an overloaded term.
> >
> While casting around for other array_* names, I remembered that
> array_keys() can take a second (and third) argument. With it, the
> function returns all keys indexing a given value.
>
> What we're discussing here is basically a dual to that: return all
> values indexed by a given key.[*]
>
> If the duality is continued, then what we get is
>
> array_values(array $input[, mixed $key])

I see what you're getting at, but in my opinion the WTF factor is
too high on this one -- the change from inspecting the outermost
elements to those a level down is too much to load on to a single
extra parameter in my opinion.

However, I do wonder if a related name may not be the way to go,
since it is similar in that both are extracting values from the
array -- perhaps:

array_inner_values($array, $key);
or
array_subvalues($array, $key);


Also, I wonder if there might be scope for multiple $key parameters,
to extract values from defined positions at even lower levels, so
that, for example:

array_whatever($array, 'colour', 'orange');

would return all sub-elements addressed as $array['colour']['orange'];


(Also, also: how would you see this proposal in relation to the
one for array_part(), should some version of that gain approval?)

Cheers!

Mike

--
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
Portland PD507, City Campus, Leeds Metropolitan University,
Portland Way, LEEDS,  LS1 3HE,  United Kingdom
E: m.ford@leedsmet.ac.uk T: +44 113 812 4730






To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm
Morgan L. Owens
Re: Re: [PHP-DEV] [DRAFT] RFC - array_column() function
June 27, 2012 01:20PM
On 2012-06-26 07:22, Ben Ramsey wrote:
>
> 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.
>
Remember that, in JavaScript, "foo.length" is equivalent to
"foo['length']". The use case for map that pluck was created to cover is
basically the same one here - essentially:

function array_{column|pluck}($array, $key)
{
return array_map(function($element)use($key)
{
return $element[$key];
}, $array);
};

Or, as it's implemented in Prototype.js (which inlines the map invocation),

function pluck(property) {
var results = [];
this.each(function(value) {
results.push(value[property]);
});
return results;
}


I will say I'm not sold on "pluck" since it describes the physical
action (a sharp tug) rather than the intended result (you pluck feathers
from a chicken but you pluck fruit from a tree). Other alternatives to
array_column that have crossed my mind include:

An extra argument to array_values() analogous to the extra argument to
array_keys(): I'm worried the analogy isn't close enough to excuse the
differences.
array_project(): too mathematically esoteric
array_select(): potentially also overloaded - what next, array_join()? -
but I admit that this is the name I typically use when I write the sort
of function that I gave above.

***

One thing about the existing implementation: it doesn't retain the keys
of the original array.

This throws away information that might still be needed. It's possible
that some elements in the original array didn't supply values to the
result (they lacked have the key in question); without the original
array's keys to provide a mapping, you won't know which ones they were.

If you did this twice on different keys, then as soon as one result
array came up short, the two sets would no longer be reconcilable.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login