From:             mtanalin at yandex dot ru
Operating system: 
PHP version:      5.5.0beta1
Package:          Arrays related
Bug Type:         Feature/Change Request
Bug description:array_column: 2nd param should be optional to use entire rows 
as result values

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 bug report at https://bugs.php.net/bug.php?id=64493&edit=1
-- 
Try a snapshot (PHP 5.4):   
https://bugs.php.net/fix.php?id=64493&r=trysnapshot54
Try a snapshot (PHP 5.3):   
https://bugs.php.net/fix.php?id=64493&r=trysnapshot53
Try a snapshot (trunk):     
https://bugs.php.net/fix.php?id=64493&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=64493&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=64493&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=64493&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=64493&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=64493&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=64493&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=64493&r=notwrong
Not enough info:            
https://bugs.php.net/fix.php?id=64493&r=notenoughinfo
Submitted twice:            
https://bugs.php.net/fix.php?id=64493&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=64493&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=64493&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=64493&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=64493&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=64493&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=64493&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=64493&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=64493&r=mysqlcfg

Reply via email to