Edit report at https://bugs.php.net/bug.php?id=64493&edit=1

 ID:                 64493
 Updated by:         ram...@php.net
 Reported by:        mtanalin at yandex dot ru
 Summary:            array_column: 2nd param should be optional to use
                     entire rows as result values
 Status:             Assigned
 Type:               Feature/Change Request
 Package:            Arrays related
 PHP Version:        5.5.0beta1
 Assigned To:        ramsey
 Block user comment: N
 Private report:     N

 New Comment:

I have added a pull request that provides this functionality.

https://github.com/php/php-src/pull/331


Previous Comments:
------------------------------------------------------------------------
[2013-03-22 17:18:00] mtanalin at yandex dot ru

Description:
------------
array_column() function (new in PHP 5.5: https://wiki.php.net/rfc/array_column 
) is a very nice addition, but its design is incomplete currently.

What is missing is the ability to have entire rows (not just a column) as 
values of each element (indexed by a specified-column's values) in array 
returned by the function.

This could be achieved by making array_column's second parameter ($columnKey) 
optional. `null` value could be used to indicate that the parameter is omitted:

        array_column($rows, null, 'id');

When both 2nd and 3nd parameters are omitted, an error should be triggered (or 
an exception thrown) usual way.

2nd parameter's optionality is needed to make it possible to quickly select 
exact row by exact column value while still having access to ALL columns of a 
row, not just one of the columns.

Otherwise, web-developers will still be forced to use pure-script workarounds 
like:

        array_combine(array_column($rows, 'id'), $rows);

Even worse, if array_column() function in its current design will be added in 
PHP 5.5.0, but the subfeature proposed here will be added in some future 
version different from exactly 5.5.0, it would then be problematic to polyfill 
it performant and future-proof way (most likely we then would be forced to rely 
on exact PHP version while relying on implementation version instead of direct 
feature detection is usually considered bad form; we can easily determine 
function existence with function_exists('array_column'), but [AFAIK] we cannot 
determine supported types of function parameters as easily.)

Hopefully, this proposal can be implemented in PHP 5.5.0 thus making design of 
array_column() more complete and more applicable in real-world web programming.

Thanks.

P.S. Of course, anyway, having array_column() even in its current design is 
much better than not having it at all.

Test script:
---------------
For example, for the following input array:

        $rows = [
                [
                        'id'    => '3',
                        'title' => 'Foo',
                        'date'  => '2013-03-25'
                ],
                [
                        'id'    => '5',
                        'title' => 'Bar',
                        'date'  => '2012-05-20'
                ]
        ];

array_column($rows, null, 'id') should return the following resulting array:

        [
                '3' => [
                        'id'    => '3',
                        'title' => 'Foo',
                        'date'  => '2013-03-25'
                ],
                '5' => [
                        'id'    => '5',
                        'title' => 'Bar',
                        'date'  => '2012-05-20'
                ]
        ]

Expected result:
----------------
Second parameter ($columnKey) of array_column() function should be optional to 
make it possible to have entire rows as values of elements (indexed by a 
specified-column's values) of returned array.

Actual result:
--------------
Second parameter is not optional, so we are forced to use pure-script 
workarounds like:

        array_combine(array_column($rows, 'id'), $rows);

to be able to quickly access exact row while still having ability to access any 
(not just one) column of the row.


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=64493&edit=1

Reply via email to