Doc #76119 [Opn]: Order of array after modification is not defined
| From: | requinix@php.net | Date: | Tue, 20 Mar 2018 15:24:36 +0000 |
| Subject: | Doc #76119 [Opn]: Order of array after modification is not defined | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-15544@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76119&edit=1
ID: 76119
Updated by: requinix@php.net
Reported by: mikko dot rantalainen at peda dot net
Summary: Order of array after modification is not defined
Status: Open
Type: Documentation Problem
-Package: Documentation problem
+Package: Arrays related
Operating System: Ubuntu Linux 16.04 LTS 64 bit
PHP Version: 7.2.3
Block user comment: N
Private report: N
New Comment:
Given that "an array in PHP is actually an ordered map" (literally the first sentence on
the page) then yes, the behavior can be trusted.
It is also spelled out in the language spec:
> The order of the elements in the map is the order in which the elements were inserted into the
> array.
https://github.com/php/php-langspec/blob/master/spec/12-arrays.md
Previous Comments:
------------------------------------------------------------------------
[2018-03-20 13:19:36] mikko dot rantalainen at peda dot net
Description:
------------
---
From manual page: http://www.php.net/language.types.array
---
As evidenced by comments in the manual (documentation) and e.g. https://stackoverflow.com/q/10914730/334451
official PHP documentation does not define order of an array after modifying the array using
following syntaxes ($a is assumed to be array with some existing data):
$a["previously-unused-key"] = "test1";
$a[] = "test2";
According to https://3v4l.org/ZTB9c all PHP implementations
since PHP 4.3.0 append "test1" and "test2" as the last items of the array $a.
However, documentation does not say if this can be trusted in future versions.
The documentation should explicitly say that the behavior is undefined (resulting array order cannot
be trusted for future versions), or it should explicitly say that using syntax
$a["unused-key"] = 1 will append a new key-value pair as the last item in the array.
(First example after subtitle "Examples" in the manual seems to suggest that this is by
design but this needs to be spelled out.)
The documentation should also do the same for the case where the key is identical to already used
key. It seems that the value is replaced in-place and the order stays intact. If this is by design,
it must be spelled out.
The way documentation is currently written, neither of these cases can be trusted to behave the same
in the future. (Note that missing documentation is about the order of the array, not about keys or
values.)
Is the test script guaranteed to output the actual results in the future, too?
Test script:
---------------
<?php
$a = array(1 => 1, 'c'=>2, 'b'=>3, 'd'=>4);
$a[] = "first value with implicit key";
unset($a['c']);
$a['a'] = "new a";
$a[] = "second value with implicit key";
$a['b'] = "replaced b";
var_export($a);
Expected result:
----------------
(Currently undefined, hopefully actual result is the expected result in future, too.)
Actual result:
--------------
array (
1 => 1,
'b' => 'replaced b',
'd' => 4,
2 => 'first value with implicit key',
'a' => 'new a',
3 => 'second value with implicit key',
)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76119&edit=1