logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: php array_merge
Author
spser



Joined: 29 Aug 2016
Posts: 97

PostPosted: Thu 28 Jan '21 4:44    Post subject: php array_merge Reply with quote

$arra = array('54984941951919'=>'aa');
$arrb = array('549849419519193'=>'bb');
echo '<pre>';
print_r( array_merge($arr, $arrb));

ret:
Array
(
[0] => aa
[1] => bb
)

Why is this happening?
Back to top
mraddi



Joined: 27 Jun 2016
Posts: 152
Location: Schömberg, Baden-Württemberg, Germany

PostPosted: Thu 28 Jan '21 9:58    Post subject: Reply with quote

Hello,

if I change the last line of your code to
Code:
print_r( array_merge($arra, $arrb));

the result on my PHP 7.3.19 is
Code:
Array
(
    [54984941951919] => aa
    [549849419519193] => bb
)

which seems to be the result you are expecting.

According to documentation at https://www.php.net/manual/en/function.array-merge.php
Quote:
... Values in the input arrays with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

If in your test the ' are missing around the keys they are treated as numeric keys which results in the return-data you've posted.
Back to top
spser



Joined: 29 Aug 2016
Posts: 97

PostPosted: Fri 29 Jan '21 3:52    Post subject: Reply with quote

I tested it under php8.0.1.
PHP 7.3.19 returns weird.
Back to top
mraddi



Joined: 27 Jun 2016
Posts: 152
Location: Schömberg, Baden-Württemberg, Germany

PostPosted: Fri 29 Jan '21 9:40    Post subject: Reply with quote

Tested again on different plattforms and:
Wow!
On my Raspberry with PHP 7.3.19 I received the results as described above.
But on other servers running 7.4.14 (directly on Windows; in Docker), 7.3.26 (in Docker), 8.0.1 (in Docker) and even 7.3.19 (in Docker, too) I get the same results as you. So my Raspberry was the only one with the expected result!

I read through the comments on PHP's documentation https://www.php.net/manual/en/function.array-merge.php and 16 years ago a person named "Alec Solway" mentioned the same behaviour we are experiencing now.
From my point of view it is a bug (at least a bug in documentation) as the behaviour does not comply with the documentation. So I have filed a bug https://bugs.php.net/bug.php?id=80685 . Let's see what is happening Smile.

As a workaround you can use the + operator in your case
Code:
print_r( $arra + $arrb );

instead of
Code:
print_r( array_merge($arra + $arrb) );
Back to top
mraddi



Joined: 27 Jun 2016
Posts: 152
Location: Schömberg, Baden-Württemberg, Germany

PostPosted: Sun 31 Jan '21 15:57    Post subject: Reply with quote

Hi,
learned something new today (from the response to the bug-report) - the behaviour IS documented in PHP's documentation - but in the general information about arrays https://www.php.net/manual/en/language.types.array.php#language.types.array.syntax.array-func (instead of within the array_merge-documentation)
Quote:
Additionally the following key casts will occur:

Strings containing valid decimal ints, unless the number is preceded by a + sign, will be cast to the int type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.

On my old Raspberry (a Raspberry2 running 32-bit-Raspbian) the keys were not casted to integers as they are outside the 32-bit-range.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Wed 19 May '21 22:32    Post subject: Reply with quote

As an addition to mraddi you may read the user comments at https://www.php.net/manual/en/function.array-merge.php
Back to top


Reply to topic   Topic: php array_merge View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner