Zend_Config_Xml strange behaviour(Zend_Config_Xml 奇怪的行为)
问题描述
我对 Zend_Config_Xml 有一个奇怪的问题.
I have a strange problem with Zend_Config_Xml.
这是一个例子.
使用这个 xml 文件 https://gist.github.com/883465
With this xml file https://gist.github.com/883465
此代码:
$config = new Zend_Config_Xml('config.xml');
var_dump($config->get('elements')->get('element')->toArray());
给出:
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"
}
}
}
}
使用这个 xml 文件 https://gist.github.com/883469
with this xml file https://gist.github.com/883469
它给出:
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"
}
}
}
我希望:
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 tricky when you want to iterate over elements
$config = new Zend_Config_Xml('config.xml');
foreach($config->get('elements')->get('element') as $element);
如果有多个元素,这很好,但如果你只有一个,你最终会遍历元素孩子!
which is fine if there are more then one elements, but if you have only one, you'll end up iterating over element children!
有什么想法吗?
我想出了一个丑陋的解决方法:
I came up with an ugly workaround:
if (0 !== $config->get('elements')->get('element')) {/