Bug #68553 [Com]: array_column: null values in $index_key become incrementing keys in result

From: Date: Mon, 04 Oct 2021 23:46:51 +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-237033@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: lemon dot head dot bw+bugs-php-net at gmail dot com Reported by: ramsey@php.net Summary: array_column: null values in $index_key become incrementing keys in result Status: Closed Type: Bug Package: Arrays related PHP Version: 5.6.3 Block user comment: N Private report: N New Comment: 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(). Previous Comments: ------------------------------------------------------------------------ [2018-11-18 00:10:35] cmb@php.net Related To: Bug #76589 ------------------------------------------------------------------------ [2018-07-24 03:37:59] 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) ------------------------------------------------------------------------ [2014-12-06 00:15:31] 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 ------------------------------------------------------------------------ [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

« previous php.bugs (#237033) next »