Suspect: Php array_merge_recursive a bug

Source: Internet
Author: User
Tags diff
$adiff = json_decode('{"28":0,"29":0,"30":0,"31":0,"32":0,"33":0,"34":0,"35":0}',true);$diff = json_decode('{"28":1,"29":1,"30":1,"31":1,"32":1,"33":1,"34":1,"35":1}',true);$adiff = array_merge_recursive($diff,$adiff);var_dump($adiff);

Theoretically should return:

array(16) { [28]=> int(1) [29]=> int(1) [20]=> int(1) [31]=> int(1) [32]=> int(1) [33]=> int(1) [34]=> int(1) [35]=> int(1) [36]=> int(0) [37]=> int(0) [38]=> int(0) [39]=> int(0) [40]=> int(0) [41]=> int(0) [42]=> int(0) [43]=> int(0) }

Actual return:

array(16) { [0]=> int(1) [1]=> int(1) [2]=> int(1) [3]=> int(1) [4]=> int(1) [5]=> int(1) [6]=> int(1) [7]=> int(1) [8]=> int(0) [9]=> int(0) [10]=> int(0) [11]=> int(0) [12]=> int(0) [13]=> int(0) [14]=> int(0) [15]=> int(0) }

Does this count as a bug?

Update:
This is not a bug, but the manual does not have a numeric key to the merge will reset the instructions clearly.

Reply content:

$adiff = json_decode('{"28":0,"29":0,"30":0,"31":0,"32":0,"33":0,"34":0,"35":0}',true);$diff = json_decode('{"28":1,"29":1,"30":1,"31":1,"32":1,"33":1,"34":1,"35":1}',true);$adiff = array_merge_recursive($diff,$adiff);var_dump($adiff);

Theoretically should return:

array(16) { [28]=> int(1) [29]=> int(1) [20]=> int(1) [31]=> int(1) [32]=> int(1) [33]=> int(1) [34]=> int(1) [35]=> int(1) [36]=> int(0) [37]=> int(0) [38]=> int(0) [39]=> int(0) [40]=> int(0) [41]=> int(0) [42]=> int(0) [43]=> int(0) }

Actual return:

array(16) { [0]=> int(1) [1]=> int(1) [2]=> int(1) [3]=> int(1) [4]=> int(1) [5]=> int(1) [6]=> int(1) [7]=> int(1) [8]=> int(0) [9]=> int(0) [10]=> int(0) [11]=> int(0) [12]=> int(0) [13]=> int(0) [14]=> int(0) [15]=> int(0) }

Does this count as a bug?

Update:
This is not a bug, but the manual does not have a numeric key to the merge will reset the instructions clearly.

The manual mentions:

If, however, the arrays has the same numeric key, the later value won't overwrite the original value, but would be AppE nded.

If the two array has the same numeric key name, a new index is created at the end of the array (although the manual does not mention that the index is reset).

It is recommended to use Array_replace_recursive (php≥5.3) in this case.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.