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
 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2019 The PHP Group
All rights reserved.
Last updated: Sun May 26 10:01:26 2019 UTC