Doc #80685 [Opn->Nab]: array_merge - odd behaviour with string keys
| From: | requinix@php.net | Date: | Fri, 29 Jan 2021 08:19:36 +0000 |
| Subject: | Doc #80685 [Opn->Nab]: array_merge - odd behaviour with string keys | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-18470@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80685&edit=1
ID: 80685
Updated by: requinix@php.net
Reported by: raddi at gmx dot net
Summary: array_merge - odd behaviour with string keys
-Status: Open
+Status: Not a bug
Type: Documentation Problem
Package: Unknown/Other Function
Operating System: Ubuntu 20.04 / Windows
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
> Yes, the keys from Test-script can be converted to integer but was defined as
> string key.
It does not matter if your code puts the keys in quotes: if it looks like a (valid) integer then it
will be treated as a integer.
https://3v4l.org/T1Jrg
> right now it is not in the mentioned documentation anymore.
It is documented in the general information about arrays:
> * Strings, die einen gültigen dezimalen Integer enthalten, es sei denn der Zahl sei ein +-
> Zeichen vorangestellt, werden zum entsprechenden Integer umgewandelt. Beispielsweise wird
> der Schlüssel "8" tatsächlich unter 8 gespeichert. Andererseits wird
> "08" nicht umgewandelt,
> da dies kein gültiger Ganzzahlwert ist.
https://www.php.net/manual/de/language.types.array.php#language.types.array.syntax.array-func
> I get this result only on my old Raspberry running PHP 7.3.19-1~deb10u1.
Those two numbers are outside the range of a 32-bit integer. Is your old Pi running 32-bit PHP?
Previous Comments:
------------------------------------------------------------------------
[2021-01-29 07:34:37] raddi at gmx dot net
Description:
------------
Test-script below is expected to output the following (according to documentation at https://www.php.net/manual/de/function.array-merge.php)
as the keys of the input-arrays are strings
Array
(
[54984941951919] => aa
[549849419519193] => bb
)
I get this result only on my old Raspberry running PHP 7.3.19-1~deb10u1.
On other servers (Windows witth 7.4.14; Docker with 7.4.14, 8.0.1, 7.3.19 and 7.3.26; even on an
Ubuntu 20.04 with 7.3.19-1~deb10u1) the result is
Array
(
[0] => aa
[1] => bb
)
with is for arrays with numeric keys.
Yes, the keys from Test-script can be converted to integer but was defined as string key.
At least I expect a consistent behaviour on all PHP-versions and a hint within the documentation
about string keys beeing convertable to integers.
I've found some older bugs (#25470, #18075, #13478, #54140) about this behaviour with #54140
stating that in the manual I should read "Strings containing valid integers will be cast to the
integer type." but right now it is not in the mentioned documentation anymore.
Test script:
---------------
$arra = array('54984941951919'=>'aa');
$arrb = array('549849419519193'=>'bb');
echo '<pre>';
print_r( array_merge($arra, $arrb) );
Expected result:
----------------
Array
(
[54984941951919] => aa
[549849419519193] => bb
)
Actual result:
--------------
Array
(
[0] => aa
[1] => bb
)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80685&edit=1