php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77276 Duplicate keys array_merge_recursive
Submitted: 2018-12-10 12:26 UTC Modified: 2018-12-10 15:23 UTC
From: mindaugas at osos dot lt Assigned: cmb (profile)
Status: Duplicate Package: Arrays related
PHP Version: 7.0.33 OS: Linux
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: mindaugas at osos dot lt
New email:
PHP Version: OS:

 

 [2018-12-10 12:26 UTC] mindaugas at osos dot lt
Description:
------------
---
From manual page: https://php.net/function.array-merge-recursive
---
When first array first element has higher index number than second element, then array_merge_recursive makes duplicate keys. In php 5 was ok, this was tested on 7.0.32 and 7.1.* - same result.

Test script:
---------------
$aray1 = [
    'service' => [
        5 => ['jehfkjfhwk'],
        2 => ['ejfoiwefoweif']
    ],
];
$aray2 = [
    'service' => [
        ['qfwwqfqwf'],
        ['wqfwfqwfqwf1'],
        ['wqfwfqwfqwf2'],
        ['wqfwfqwfqwf3'],
        ['wqfwfqwfqwf4'],
        ['wqfwfqwfqwf5'],
        ['wqfwfqwfqwf6'],
    ],
];

echo '<pre>';
print_r(array_merge_recursive($aaray1, $aaray2));

Expected result:
----------------
Array
(
    [service] => Array
        (
            [5] => Array
                (
                    [0] => jehfkjfhwk
                )

            [2] => Array
                (
                    [0] => ejfoiwefoweif
                )

            [6] => Array
                (
                    [0] => qfwwqfqwf
                )

            [7] => Array
                (
                    [0] => wqfwfqwfqwf1
                )

            [8] => Array
                (
                    [0] => wqfwfqwfqwf2
                )

            [9] => Array
                (
                    [0] => wqfwfqwfqwf3
                )

            [10] => Array
                (
                    [0] => wqfwfqwfqwf4
                )

            [11] => Array
                (
                    [0] => wqfwfqwfqwf5
                )

            [12] => Array
                (
                    [0] => wqfwfqwfqwf6
                )

        )

)

Actual result:
--------------
Array
(
    [service] => Array
        (
            [5] => Array
                (
                    [0] => jehfkjfhwk
                )

            [2] => Array
                (
                    [0] => ejfoiwefoweif
                )

            [2] => Array
                (
                    [0] => qfwwqfqwf
                )

            [3] => Array
                (
                    [0] => wqfwfqwfqwf1
                )

            [4] => Array
                (
                    [0] => wqfwfqwfqwf2
                )

            [5] => Array
                (
                    [0] => wqfwfqwfqwf3
                )

            [6] => Array
                (
                    [0] => wqfwfqwfqwf4
                )

            [7] => Array
                (
                    [0] => wqfwfqwfqwf5
                )

            [8] => Array
                (
                    [0] => wqfwfqwfqwf6
                )

        )

)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-12-10 12:31 UTC] mindaugas at osos dot lt
-Status: Open +Status: Closed
 [2018-12-10 12:31 UTC] mindaugas at osos dot lt
Fixed as of Version 7.2.8
#Fixed bug #76505 (array_merge_recursive() is duplicating sub-array keys).
 [2018-12-10 15:23 UTC] cmb@php.net
-Status: Closed +Status: Duplicate -Assigned To: +Assigned To: cmb
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 16:01:29 2024 UTC