简体   繁体   中英

Odd array_merge_recursive behavior with string keys

I'm trying to use array_merge_recursive to merge two data structures.

<?php
$testSite = array(
    'name' => 'test site',
    'modules' => array(
        'foo' => 'true',
        'bar' => 'true'
    )
);
$testData = array(
    'modules' => array(
        'bar' => 'false'
    )
);

$testSite = array_merge_recursive($testSite, $testData);

Note that I'm using strings instead of booleans for debug printing purposes

I would expect $testSite to be the exact same after this code has ran, except for the modules.bar property, which I'd expect to see being changed to false . What happens instead, as seen in this live example , is that bar is turned into an array containing it's old value and the value false is appended to that.

The documentation page reads that this is what will happen for numeric keys, but these are all strings keys. Can anyone shed some light on this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM