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 奇怪的行为
猜你喜欢
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
