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

Issue Type: Bug Created: 2012-07-20T10:54:44.000+0000 Last Updated: 2012-12-22T12:35:37.000+0000 Status: Closed Fix version(s): Reporter: Joshua Stoutenburg (jehoshua02) Assignee: Rob Allen (rob) Tags: - Zend_Config

Related issues: Attachments:


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.


Posted by Joshua Stoutenburg (jehoshua02) on 2012-07-20T11:11:11.000+0000

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


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

Posted by Joshua Stoutenburg (jehoshua02) on 2012-07-27T22:11:17.000+0000

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

Posted by Rob Allen (rob) on 2012-12-22T12:35:37.000+0000

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.

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.