本文介绍了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')) {//}

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

这有助于我确定元素标签下是否有多个元素.

This helps me to identify if there is more then one elements under elements tag.

很丑.

更聪明?

推荐答案

看起来 Zend_Config_Xml 显式地折叠了这样的 1-element 集合(源代码中有一个 if 语句可以做到这一点).一些可能的解决方法:

It seems that Zend_Config_Xml explicitly collapses such 1-element collections (there's an if statement in the source that does this). Some possible workarounds:

  • 重载 Zend_Config_Xml 并修复加载器代码,使其不会折叠 1 元素集合
  • 重载 Zend_Config_Xml 并重载 get() 以更简洁的方式包含您丑陋的解决方法.
  • 使用 SimpleXML 代替 Zend_Config_Xml
  • Overload Zend_Config_Xml and fix the loader code so it doesn't collapse 1-element collections
  • Overload Zend_Config_Xml and overload get() to include your ugly workaround in a cleaner way.
  • Use SimpleXML instead of Zend_Config_Xml

这篇关于Zend_Config_Xml 奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 00:56