Req #66435 [Asn->Wfx]: Suggestion: Preserve original index in function array_column()
Edit report at https://bugs.php.net/bug.php?id=66435&edit=1
ID: 66435
Updated by: ramsey@php.net
Reported by: djot at hotmail dot com
Summary: Suggestion: Preserve original index in function
array_column()
-Status: Assigned
+Status: Wont fix
Type: Feature/Change Request
Package: Arrays related
PHP Version: Irrelevant
Assigned To: ramsey
Block user comment: N
Private report: N
New Comment:
This has been sitting around forever. Any change would break existing expected behavior, so closing.
Previous Comments:
------------------------------------------------------------------------
[2015-11-22 21:28:16] 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)
------------------------------------------------------------------------
[2014-12-05 16:09:07] ramsey@php.net
See also discussion here on the userland polyfill for array_column():
https://github.com/ramsey/array_column/issues/3
------------------------------------------------------------------------
[2014-01-07 14:57:45] 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');
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66435&edit=1
Thread (5 messages)