Config
Config copied to clipboard
Index arrays in config is not overrided correctly
Hello! Trying to use your lib, but faced a problem:
require 'vendor/autoload.php';
use PHLAK\Config\Config;
$array1 = [
'assoc_key' => 'testvalue',
'key' => [1,2,3,4],
];
$array2 = [
'assoc_key' => 'override',
'key' => ['somevalue'],
];
$config1 = new Config($array1);
$config2 = new Config($array2);
print_r($config1->merge($config2)->toArray());
It returns:
Array
(
[assoc_key] => override
[key] => Array
(
[0] => somevalue
[1] => 2
[2] => 3
[3] => 4
)
)
But should return:
Array
(
[assoc_key] => override
[key] => Array
(
[0] => somevalue
)
)
This is the intended functionality however, I understand the confusion. The reason for this is due to the use of array_replace_recursive for merging the config arrays (see here). This is necessary to support merging of multi-dimensional arrays properly. For example:
$one = new Config([
'database' => [
'server' => '192.168.0.10',
'port' => 3306
]
]);
$two = new Config([
'database' => [
'server' => '10.10.0.5'
]
]);
$one->merge($two);
Results in the following config array:
[
'database' => [
'server' => '10.10.0.5',
'port' => 3306
]
]
This unfortunately breaks when the value of the top-level option is an array that is intended to be treated as an individual value. I don't currently have a work around for this but will keep this issue open to remind me to think about it and hopefully find a long-term solution.