Bug #68553 [Com]: array_column: null values in $index_key become incrementing keys in result
| From: | ramsey@php.net | Date: | Sat, 06 Dec 2014 00:15:31 +0000 |
| Subject: | Bug #68553 [Com]: array_column: null values in $index_key become incrementing keys in result | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-188943@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68553&edit=1
ID: 68553
Comment by: ramsey@php.net
Reported by: ramsey@php.net
Summary: array_column: null values in $index_key become
incrementing keys in result
Status: Open
Type: Bug
Package: Arrays related
PHP Version: 5.6.3
Block user comment: N
Private report: N
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2014-12-05 17:22:52] 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-05 16:46:04] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68553&edit=1