|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-12-13 21:27 UTC] 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) }
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 22:00:01 2025 UTC |
I guess you even meant: Expected result: ---------------- array(4) { [1]=> int(1) [2]=> int(4) [3]=> int(9) ["four"]=> int(16) } (i.e. keep all keys, even numeric) and I agree with you! (and a quick Web search shows we're not alone...) --- @requinix: I see at least 2 ways: 1. Add a `bool $preserve_keys = false` parameter (like array_slice) that would at least enable us to use array_column($items, "id", null, true) 2. Change the default behavior (consistent with array_map) and just use array_values(array_column($items, "id")) if you really want to lose the keys PS: see also Request #66435