Req #74100 [Opn]: array_fill with negative $start_index works incorrectly

From: Date: Sun, 08 Jul 2018 19:57:39 +0000
Subject: Req #74100 [Opn]: array_fill with negative $start_index works incorrectly
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-216234@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74100&edit=1 ID: 74100 Updated by: cmb@php.net Reported by: andrew dot nester dot dev at gmail dot com Summary: array_fill with negative $start_index works incorrectly Status: Open Type: Feature/Change Request Package: Arrays related Operating System: Any PHP Version: Irrelevant Block user comment: N Private report: N New Comment: Note that the behavior will change in PHP 8 due to <https://wiki.php.net/rfc/negative_array_index>. Previous Comments: ------------------------------------------------------------------------ [2017-03-04 16:15:15] rowan dot collins at gmail dot com While poorly documented, this is definitely not a bug, and is actually consistent with the rest of the language. In particular, consider that the following all produce the same result: $explicit = [-10 => true, true, true]; $fill = array_fill(-10, 3, true); $push = [-10 => true]; $push[] = true; $push[] = true; The logic is that appending to an array always uses the next non-negative integer as the key. Changing this would affect much more than just array_fill, and would need an RFC justifying the language change and considering its impact on existing code. ------------------------------------------------------------------------ [2017-02-15 09:42:32] andrew dot nester dot dev at gmail dot com Also I added PR with fix for this behaviour. ------------------------------------------------------------------------ [2017-02-15 09:33:57] andrew dot nester dot dev at gmail dot com Description: ------------ When we are using array_fill function with negative $start_index it produces wrong array result. As I see this is documented behaviour (http://php.net/manual/en/function.array-fill.php#function.array-fill.example.basic ). Also there is note in documentation: "See also the Arrays section of manual for a detailed explanation of negative keys." But there is no any information about negative keys in arrays and why it should work like this. We can support negative indexes in arrays now that's why it's better to make array_fill work as expected. Test script: --------------- var_dump(array_fill_keys(range(-2,1), true)); var_dump(array_fill(-2, 4, true)) Expected result: ---------------- array(4) { [-2]=> bool(true) [-1]=> bool(true) [0]=> bool(true) [1]=> bool(true) } array(4) { [-2]=> bool(true) [-1]=> bool(true) [0]=> bool(true) [1]=> bool(true) } Actual result: -------------- array(4) { [-2]=> bool(true) [-1]=> bool(true) [0]=> bool(true) [1]=> bool(true) } array(4) { [-2]=> bool(true) [0]=> bool(true) [1]=> bool(true) [2]=> bool(true) } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=74100&edit=1

« previous php.bugs (#216234) next »