ZF-12342: Zend_Config->merge() does not behave as expected on numerically indexed arrays

Description

Maybe this behavior is somehow justified, however, it was not what I was expecting. When I merge configs containing numerically indexed arrays, the items from one array overwrite, in the other array, the items that have the same numerical index. I would have expected it to work more like PHP {{array_merge()}}, whose documentation reads:

| If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

Here is a code snippet to reproduce the issue:

| $config1 = new Zend_Config(array('item1', 'item2'), true); $config2 = new Zend_Config(array('item3')); $configMerged = $config1->merge($config2); var_dump($configMerged->toArray());

The result:

| array (size=2) 0 => string 'item3' (length=5) 1 => string 'item2' (length=5)

I was expecting:

| array (size=3) 0 => string 'item1' (length=5) 1 => string 'item2' (length=5) 2 => string 'item3' (length=5)

This issue affects at least 1.10.6.

Comments

While waiting, I came up with a cute work around:

| $config1 = new Zend_Config(array('item1', 'item2'), true); $config2 = new Zend_Config(array('item3')); $configMerged = new Zend_Config(array_merge($config1->toArray(), $config2->toArray())); var_dump($configMerged);

Result:

| array (size=3) 0 => string 'item1' (length=5) 1 => string 'item2' (length=5) 2 => string 'item3' (length=5)

Turns out my workaround is deficient. Due to the behavior of array_merge(), the indexes are overwritten rather than actually merged.

The behaviour that you report is as expected as otherwise, there would be no way to override the first element in an array from a child section.