php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #80685 array_merge - odd behaviour with string keys
Submitted: 2021-01-29 07:34 UTC Modified: 2021-01-29 08:19 UTC
From: raddi at gmx dot net Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: Irrelevant OS: Ubuntu 20.04 / Windows
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: raddi at gmx dot net
New email:
PHP Version: OS:

 

 [2021-01-29 07:34 UTC] 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
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-01-29 08:19 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2021-01-29 08:19 UTC] requinix@php.net
> 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?
 [2021-01-31 13:53 UTC] raddi at gmx dot net
You are absolutely right - the "old Raspberry" (it is a Raspberry2) is running a 32-bit Raspbian.
And you are right about the documentation in the general section about arrays. I was looking for this hint within the documentation for array_merge.
Learned something new (the automatic conversion of the keys).
Thank you and sorry for any inconvenience.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 17:01:32 2024 UTC