Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index arrays in config is not overrided correctly #7

Open
andser opened this issue Dec 6, 2018 · 1 comment
Open

Index arrays in config is not overrided correctly #7

andser opened this issue Dec 6, 2018 · 1 comment
Assignees

Comments

@andser
Copy link

andser commented Dec 6, 2018

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
        )

)
@PHLAK
Copy link
Owner

PHLAK commented Dec 7, 2018

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.

@PHLAK PHLAK self-assigned this Mar 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants