合并两个Zend_Config_Ini实例会覆盖在第一个对象中定义的数组

时间:2012-03-01 20:23:57

标签: zend-framework merge zend-config

给出两个* .ini文件:

one.ini

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

[development : production]

two.ini

[production]
someArray[] = 'four'

[development : production]

将两个* .ini文件加载为Zend_Config_Ini实例

$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());

合并后的输出:

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

)

是否可以合并数组,输出方式如下图所示?

我知道可以通过在每个* .ini文件中的数组上定义数字索引来完成,但我想尽可能避免这种情况。

//Ideal merge results

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

)

2 个答案:

答案 0 :(得分:1)

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

应该这样做。

答案 1 :(得分:0)

您可以执行以下两种解决方案之一:

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

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

var_dump($config->asArray());