php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #74100 array_fill with negative $start_index works incorrectly
Submitted: 2017-02-15 09:33 UTC Modified: 2020-03-04 10:33 UTC
From: andrew dot nester dot dev at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Arrays related
PHP Version: Irrelevant OS: Any
Private report: No CVE-ID: None
 [2017-02-15 09:33 UTC] 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)
}

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-02-15 09:42 UTC] andrew dot nester dot dev at gmail dot com
Also I added PR with fix for this behaviour.
 [2017-02-15 10:01 UTC] requinix@php.net
-Type: Bug +Type: Feature/Change Request
 [2017-03-04 16:15 UTC] 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.
 [2018-07-08 19:57 UTC] cmb@php.net
Note that the behavior will change in PHP 8 due to
<https://wiki.php.net/rfc/negative_array_index>.
 [2020-03-04 10:33 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2020-03-04 10:33 UTC] nikic@php.net
Closing as this has been fixed in PHP 8.0.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 02:02:52 2024 UTC