开发者

misleading usage of square bracket in SimpleXMLElement object

I found the [] operator is sometimes confusing when it is used agains SimpleXMLElement object.

$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)

I can't find any documents about the numeric indexing usage of SimpleXmlElement 开发者_JAVA百科class. Can anybody explain how those two worked?

Note that it seems this [num] operator of SimpleXmlElement just mimic the behavior of Array. I feel that this is not something stirred with Array, but the implementation of SimpleXmlElement class.


I don't believe anything magical is going on here. An array in PHP can keyed by an integer, and may be keyed by a string as well. So the $xml->children() line is likely making an array of key-value attribute pairs in the form

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜