Zend_Config_Xml weird behavior

I have a strange problem with Zend_Config_Xml.

Here is an example.

With this xml file https://gist.github.com/883465

this code:

$config = new Zend_Config_Xml('config.xml'); var_dump($config->get('elements')->get('element')->toArray()); 

gives:

 array(2) { [0]=> array(2) { ["a"]=> array(1) { ["attr"]=> string(2) "at" } ["e"]=> array(3) { [0]=> array(1) { ["attr"]=> string(2) "at" } [1]=> array(1) { ["attr"]=> string(2) "at" } [2]=> array(1) { ["attr"]=> string(2) "at" } } } [1]=> array(2) { ["a"]=> array(1) { ["attr"]=> string(2) "at" } ["e"]=> array(3) { [0]=> array(1) { ["attr"]=> string(2) "at" } [1]=> array(1) { ["attr"]=> string(2) "at" } [2]=> array(1) { ["attr"]=> string(2) "at" } } } } 

with this xml file https://gist.github.com/883469

He gives:

 array(2) { ["a"]=> array(1) { ["attr"]=> string(2) "at" } ["e"]=> array(3) { [0]=> array(1) { ["attr"]=> string(2) "at" } [1]=> array(1) { ["attr"]=> string(2) "at" } [2]=> array(1) { ["attr"]=> string(2) "at" } } } 

and I expect:

 array(1) { [0]=> array(2) { ["a"]=> array(1) { ["attr"]=> string(2) "at" } ["e"]=> array(3) { [0]=> array(1) { ["attr"]=> string(2) "at" } [1]=> array(1) { ["attr"]=> string(2) "at" } [2]=> array(1) { ["attr"]=> string(2) "at" } } } } 

This is difficult if you want to iterate over the elements.

 $config = new Zend_Config_Xml('config.xml'); foreach($config->get('elements')->get('element') as $element); 

which is great if there is more than one element, but if you have only one, you will end the iteration over the children of the element!

Any idea?

EDIT:

I came up with an ugly workaround:

if (0! == $ config-> get ('elements') → get ('element')) {//}

This helps me determine if there is more than one element in the element tag.

Very ugly.

Is smarting smarter?

+2
source share
1 answer

It seems that Zend_Config_Xml explicitly dumps such singleton collections (there is an if in the source that does this). Possible workarounds:

  • Reload Zend_Config_Xml and fix the bootloader code so that it does not drop 1-element collections
  • Overloading Zend_Config_Xml and overloading get() to enable your ugly workaround in a cleaner way.
  • Use SimpleXML instead of Zend_Config_Xml
+1
source

Source: https://habr.com/ru/post/1399961/


All Articles