Alex Turpin
Alex Turpin

Reputation: 47776

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?

Upvotes: 1

Views: 167

Answers (1)

Related Questions