This is an old revision of the document!
PHP RFC: Arrays starting with a negative index
- Version: 0.1
- Date: 2017-04-20
- Author: Pedro Magalhães, mail at pmmaga dot com
- Status: Draft
- First Published at: http://wiki.php.net/rfc/negative_array_index
Introduction
Currently, 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
.
Proposal
As 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:
<?php $a = array_fill(-2, 3, true); $b = [-2 => true, true, true]; $c = ["string" => true, -2 => true, true, true]; unset($c["string"]); $d[-2] = true; $d[] = true; $d[] = true;
Result in the array:
array(3) { [-2]=> bool(true) [0]=> bool(true) [1]=> bool(true) }
With the implementation of this RFC, they will all result in:
array(3) { [-2]=> bool(true) [-1]=> bool(true) [0]=> bool(true) }
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:
<?php $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 7.2
Unaffected PHP Functionality
Arrays with explicit keys, string keys, or an initial numeric index >= 0
are not affected.
Furthermore, iterating over arrays without explicit keys (ie. foreach
) is also not affected.
Proposed Voting Choices
This RFC requires a 2/3 majority.
Patches and Tests
This RFC is implemented by the following patches:
- Implementation: https://github.com/php/php-src/pull/2383
- Documentation: WIP
References
Discussion on the mailing list:https://externals.io/thread/712