Mike Moore
Mike Moore

Reputation: 7478

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
        )

)

Upvotes: 2

Views: 2556

Answers (2)

Weltschmerz
Weltschmerz

Reputation: 2186

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

That should do it.

Upvotes: 1

Mouna Cheikhna
Mouna Cheikhna

Reputation: 39678

You can do one of these two solutions :

$config = new Zend_Config($two->asArray() + $one->asArray()); 
var_dump($config->asArray()); 

or

$config = new Zend_Config(array_merge($one->asArray(),  $two->asArray()));  

var_dump($config->asArray()); 

Upvotes: 0

Related Questions