Req #73735 [Opn]: array_column keys should be preserved
| From: | requinix@php.net | Date: | Wed, 14 Dec 2016 03:23:59 +0000 |
| Subject: | Req #73735 [Opn]: array_column keys should be preserved | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-205982@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=73735&edit=1
ID: 73735
Updated by: requinix@php.net
Reported by: photon0 at gmail dot com
Summary: array_column keys should be preserved
Status: Open
Type: Feature/Change Request
Package: Arrays related
Operating System: ANY
PHP Version: Next Minor Version
Block user comment: N
Private report: N
New Comment:
As default behavior it makes sense, but it should be possible to explicitly invoke the behavior
using arguments and I'm not sure how the change could be squeezed into the function. NULL as
the $index_key?
Anyway, the workaround is trivial:
array_combine(array_keys($items), array_column($items, "id"))
Previous Comments:
------------------------------------------------------------------------
[2016-12-13 21:27:05] photon0 at gmail dot com
Description:
------------
It would increase the usability of this function if it could preserve the array's keys in the
result.
I was expecting this to be the default behaviour.
best regards,
David Lévêsque
Test script:
---------------
$items = array(
1 => ["id" => 1],
2 => ["id" => 4],
3 => ["id" => 9],
"four" => ["id" => 16]
);
var_dump(array_column($items,"id"));
Expected result:
----------------
array(4) { [0]=> int(1) [1]=> int(4) [2]=> int(9) ["four"]=> int(16) }
Actual result:
--------------
array(4) { [0]=> int(1) [1]=> int(4) [2]=> int(9) [3]=> int(16) }
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=73735&edit=1