php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #66435 Suggestion: Preserve original index in function array_column()
Submitted: 2014-01-07 14:57 UTC Modified: 2017-07-23 14:47 UTC
Votes:5
Avg. Score:4.0 ± 0.6
Reproduced:5 of 5 (100.0%)
Same Version:4 (80.0%)
Same OS:2 (40.0%)
From: djot at hotmail dot com Assigned: ramsey (profile)
Status: Wont fix Package: Arrays related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
35 - 2 = ?
Subscribe to this entry?

 
 [2014-01-07 14:57 UTC] djot at hotmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.array-column
---
PHP v5.5.0 and above
---


Assuming you have an ARRAY like this, you cannot preserve 'original_index':
$arr['original_index'] = ARRAY('new_index'=>'foo', 'value'=>'bar');

// => results in lost original_index:
$arr = array_column($arr, NULL, 'new_index');
$arr['foo'] = ARRAY('new_index'=>'foo', 'value'=>'bar');



Suggestion: Option, to preserve original index:

array array_column ( array $array , mixed $column_key [, mixed $index_key = null ] [, mixed $column_index_original] )

// => "Push" original index as column 'old_index' to the ARRAY.
$arr = array_column($arr, NULL, 'new_index', 'old_index');
$arr['foo'] = ARRAY('new_index'=>'foo', 'value'=>'bar', 'old_index'=>'original_index');



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-12-05 16:08 UTC] ramsey@php.net
-Assigned To: +Assigned To: ramsey
 [2014-12-05 16:09 UTC] ramsey@php.net
See also discussion here on the userland polyfill for array_column():
https://github.com/ramsey/array_column/issues/3
 [2015-11-22 21:28 UTC] ww dot galen at gmail dot com
During the vote discussion (http://marc.info/?l=php-internals&m=135802311120268), Peter Cowburn suggested the 3rd parameter also take a bool, with `TRUE` meaning preserve keys and `FALSE` meaning re-key. This achieves the desired behavior without adding an argument. Here's the breakdown from his post:

> So the idea is to potentially change the $indexKey parameter from
string|int to bool|string|int where:
> - false => same as null or not using the argument: uses 0-based keys
(as RFC currently does)
> - true => uses keys from $input (suggested addition)
> - int|string => uses sub-array's $indexKey'th value as key (or 0-based
for missing value, as RFC currently does)
 [2017-07-23 14:47 UTC] ramsey@php.net
-Status: Assigned +Status: Wont fix
 [2017-07-23 14:47 UTC] ramsey@php.net
This has been sitting around forever. Any change would break existing expected behavior, so closing.
 [2017-08-11 20:13 UTC] ww dot galen at gmail dot com
At least two designs, adding a parameter and expanding the domain of the `$index_key` parameter, shouldn't break existing behavior.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Sep 18 01:03:38 2021 UTC