php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64493 array_column: 2nd param should be optional to use entire rows as result values
Submitted: 2013-03-22 17:18 UTC Modified: 2014-12-05 16:06 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: mtanalin at yandex dot ru Assigned: ramsey (profile)
Status: Not a bug Package: Arrays related
PHP Version: 5.5.0beta1 OS:
Private report: No CVE-ID: None
 [2013-03-22 17:18 UTC] 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.

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-03-23 03:06 UTC] laruence@php.net
-Assigned To: +Assigned To: ramsey
 [2013-04-22 17:26 UTC] ramsey@php.net
I have added a pull request that provides this functionality.

https://github.com/php/php-src/pull/331
 [2014-12-05 16:06 UTC] ramsey@php.net
-Status: Assigned +Status: Not a bug
 [2014-12-05 16:06 UTC] ramsey@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

See comments on aforementioned pull request link. After discussion with internals, we decided not to include this functionality.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 08:01:28 2024 UTC