Req #66435 [Com]: Suggestion: Preserve original index in function array_column()

From: Date: Fri, 11 Aug 2017 20:13:17 +0000
Subject: Req #66435 [Com]: Suggestion: Preserve original index in function array_column()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-210616@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66435&edit=1 ID: 66435 Comment by: ww dot galen at gmail dot com Reported by: djot at hotmail dot com Summary: Suggestion: Preserve original index in function array_column() 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: At least two designs, adding a parameter and expanding the domain of the $index_key parameter, shouldn't break existing behavior. Previous Comments: ------------------------------------------------------------------------ [2017-07-23 14:47:13] ramsey@php.net This has been sitting around forever. Any change would break existing expected behavior, so closing. ------------------------------------------------------------------------ [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

« previous php.bugs (#210616) next »