php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68553 array_column: null values in $index_key become incrementing keys in result
Submitted: 2014-12-05 16:46 UTC Modified: -
Votes:3
Avg. Score:3.7 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: ramsey@php.net Assigned:
Status: Closed Package: Arrays related
PHP Version: 5.6.3 OS:
Private report: No CVE-ID: None
 [2014-12-05 16:46 UTC] ramsey@php.net
Description:
------------
When using $index_key to index the resulting array by a column from the original array, if null values are encountered, the resulting keys become integer values that are auto-incremented according to the previous integer key value (or start from 0, as a 0-indexed key).

Test script with actual result and expected result is available here:
https://gist.github.com/ramsey/da33c07d95bfd58ea29a


Jan Roald Haugland first reported this issue on my blog:
http://benramsey.com/blog/2013/03/introducing-array-column-in-php-5-dot-5/#comment-1660885919

Test script:
---------------
See link to gist.

Expected result:
----------------
NULL keys should be converted to empty strings, and multiple matching keys should be overwritten by the last value entered for that key.

For example:

$foo = [
    null => 1,
    null => 2,
    null => 3
];

becomes

[
    '' => 3
]

Actual result:
--------------
With array_column(), NULL keys are being converted to 0-indexed integer key values, incrementing.

So an array that gets evaluated to this and, therefore, should result in the expected array shown above:

[
    null => 1,
    null => 2,
    null => 3
]

ends up resulting in this:

[
    0 => 1,
    1 => 2,
    2 => 3
]

See link to gist for full example and actual output.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-12-05 17:22 UTC] ramsey@php.net
As a side note, I tested this on HHVM 3.4.0, and it properly produces the expected output, so we need to fix PHP's engine to produce the same expected output.
 [2014-12-06 00:15 UTC] ramsey@php.net
My array_column polyfill library also behaves as expected on earlier versions of PHP (specifically tested the example script on 5.3.29):

https://github.com/ramsey/array_column
 [2018-07-24 03:37 UTC] laruence@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=38d97557da2614f5fe1d0fa64f33dfb2cf6cbaad
Log: Fixed bug #68553 (array_column: null values in $index_key become incrementing keys in result)
 [2018-07-24 03:37 UTC] laruence@php.net
-Status: Open +Status: Closed
 [2021-10-04 23:46 UTC] lemon dot head dot bw+bugs-php-net at gmail dot com
Especially frustrating if subsequent explicit items overwrite previous auto-increment items.
See https://3v4l.org/H1Yee
Typical example would be doing this with token_get_all().
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 07:01:29 2024 UTC