代码之家  ›  专栏  ›  技术社区  ›  solotim

simplexmlement对象中方括号的误用

  •  1
  • solotim  · 技术社区  · 14 年前

    $level_a = $xml->children();
    $level_a['name'];    # this returns the 'name' attribute of level_a (SimpleXmlElement object)
    $level_a[0];         # this returns $level_a itself!
    $level_a[1];         # this returns the second SimpleXmlElement object under root node. (Same level as level_a)
    

    我找不到任何关于simplexmlement类的数字索引用法的文档。有人能解释一下这两个人是怎么工作的吗?

    注意,simplexmlement的这个[num]操作符似乎只是模仿数组的行为。我觉得这不是数组引起的,而是simplexmlement类的实现。

    1 回复  |  直到 14 年前
        1
  •  1
  •   J Cooper    14 年前

    我不相信这里发生了什么神奇的事情。PHP中的数组可以由整数设置键,也可以由字符串设置键。所以 $xml->children() 行很可能在表单中生成一个键值属性对数组

    foreach (attrs($element) as $attribute_name => $attribute_value)
        $array[$attribute_name] = $attribute_value;
    $array[0] = $element;
    // etc.