This is an old revision of the document!

PHP RFC: Arrays starting with a negative index


On the documentation for array_fill the start_index description mentions: “If start_index is negative, the first index of the returned array will be start_index and the following indices will start from zero.” This is consistent everywhere when an explicit negative key is used and implicit keys are used afterwards.


In other words, any array that has a number n as it's first numeric key will have for it's next implicit key either n+1 if n >= 0 or 0 if n < 0. This RFC proposes to make this consistent by always using n+1 regardless of the sign of n.

As also mentioned on the relevant part of the arrays documentation:

if no key is specified, the maximum of the existing integer indices is taken, and the new key will be that maximum value plus 1 (but at least 0).

This RFC proposes to eliminate the parenthesis part of that sentence. Which is an exception to a rule, that people have to learn one way or the other.

All the variables in this code:

$a = array_fill(-2, 3, true);
$b = [-2 => true, true, true];
$c = ["string" => true, -2 => true, true, true];
$d[-2] = true;
$d[] = true;
$d[] = true;

Result in the array:

array(3) {

NOTE: During the deprecation phase the following E_DEPRECATED notice will be emitted in cases where the behavior will change:

Deprecated: In the next major version of PHP the implicit keys of this array will start from -1 instead of 0 in ...

With the implementation of this RFC, they will all result in:

array(3) {

Backward Incompatible Changes

Code that relies on the current behavior to access array elements with an explicit key after that array has been started with a negative index and used implicit keys afterwards will change it's behavior. Example:

$a[-2] = true;
$a[] = true;
$a[] = true;
if ($a[1] === true) {
    echo 'Save the world.';

Will no longer save the world.

Proposed PHP Version(s)

PHP 8.0

Unaffected PHP Functionality

Arrays with explicit keys, string keys, or an initial numeric index >= 0 are not affected. This also means that arrays that never use explicit keys will still start with 0.

Furthermore, iterating over arrays without explicit keys (ie. foreach) is not affected.

Voting Choices

This RFC requires a 2/3 majority.

Voting opened on 13/6/2017 19:00 UTC and will close on 28/6/2017 19:00 UTC.

Voting suspended pending discussion on the deprecation notice

Arrays starting with a negative index for PHP 8.0
Real name Yes No
dm (dm)  
kalle (kalle)  
nikic (nikic)  
ocramius (ocramius)  
peehaa (peehaa)  
pollita (pollita)  
zeev (zeev)  
Final result: 4 3
This poll has been closed.

The previous vote targeting 7.2 was closed with 14 for and 16 against

Patches and Tests

This RFC is implemented by the following patches:

  1. Documentation: WIP


Discussion on the mailing list: https://externals.io/thread/712

First proposal Voting period discussion: https://externals.io/thread/928

rfc/negative_array_index.1506086901.txt.gz · Last modified: 2017/09/22 13:28 by