2

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
        )

)
Mike Moore
  • 7,228
  • 10
  • 42
  • 63

2 Answers2

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

That should do it.

Weltschmerz
  • 2,166
  • 1
  • 15
  • 18
0

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()); 
Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69
  • Neither of these work for me. I should have stated I am using ZF version 1.11. `asArray()` is not an available method. Assuming you meant `toArray()`, this does not work either. – Mike Moore Mar 01 '12 at 20:51