Zend_Config_Xml strange behaviour(Zend_Config_Xml 奇怪的行为)
本文介绍了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')) {/
沃梦达教程
本文标题为:Zend_Config_Xml 奇怪的行为
基础教程推荐
猜你喜欢
- Web 服务器如何处理请求? 2021-01-01
- php中的PDF导出 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- php中的foreach复选框POST 2021-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
