Search code examples
phpmergezend-frameworkzend-config

Merging two Zend_Config_Ini instances overwrites arrays defined in first object


Given two *.ini files:

one.ini

[production]
someArray[] = 'one'
someArray[] = 'two'
someArray[] = 'three'

[development : production]

two.ini

[production]
someArray[] = 'four'

[development : production]

Load both *.ini files as Zend_Config_Ini instances

$one = new Zend_Config_Ini(
    APPLICATION_PATH . "/configs/one.ini",
    APPLICATION_ENV,
    array('allowModifications' => true)
);

$two = new Zend_Config_Ini(
    APPLICATION_PATH . "/configs/two.ini",
    APPLICATION_ENV,
    array('allowModifications' => true)
);

$one->merge($two);
print_r($one->toArray());

Output after merge:

Array
(
    [someArray] => Array
        (
            [0] => four
            [1] => two
            [2] => three
        )

)

Is it possible to merge the arrays in a way that the output would be like the example below?

I know it can be done by defining numerical indices on the arrays in each *.ini file, but I would like to avoid this, if possible.

//Ideal merge results

Array
(
    [someArray] => Array
        (
            [0] => one
            [1] => two
            [2] => three
            [3] => four
        )

)

Solution

  • $new = new Zend_Config(array_merge_recursive($one->toArray(), $two->toArray()));
    var_dump($new->toArray());
    

    That should do it.