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?
source share