Reputation: 47776
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
Reputation: 2083
I think you want array_replace_recursive
.
array_merge_recursive()
vs. array_replace_recursive()
Upvotes: 3