Php foreach对象迭代,其属性包含'@';
这里是Php foreach对象迭代,其属性包含'@';,php,arrays,object,foreach,Php,Arrays,Object,Foreach,这里是$v的转储,其中包含对象 object(SimpleXMLElement)[69] public '@attributes' => array 'identifier' => string 'FC7C5117-8FF9-4FF4-86D2-F139EDE6EA74-19726-00011178F6D7A5AC' (length=59) 'fileext' => string 'pdf' (length=3) public 'tit
$v
的转储,其中包含对象
object(SimpleXMLElement)[69]
public '@attributes' =>
array
'identifier' => string 'FC7C5117-8FF9-4FF4-86D2-F139EDE6EA74-19726-00011178F6D7A5AC' (length=59)
'fileext' => string 'pdf' (length=3)
public 'title' => string 'The PDF File' (length=12)
public 'summary' => string 'Summary for the pdf file stuff' (length=30)
public 'tags' =>
object(SimpleXMLElement)[70]
public 'tag' => string 'PDFTag' (length=6)
public 'timeSignature' =>
object(SimpleXMLElement)[71]
public '@attributes' =>
array
'upper' => string '4' (length=1)
'lower' => string '4' (length=1)
public 'key' => string 'C' (length=1)
public 'transposition' => string 'PDF Trans' (length=9)
public 'bpm' => string '120' (length=3)
public 'defaultAudio' => string '57895336-6D03-41B4-954C-91DA3F512185-19726-00011178DFE613C5' (length=59)
我知道
它从title
title
object(SimpleXMLElement)[74]
string 'The PDF File' (length=12)
summary
object(SimpleXMLElement)[72]
string 'Summary for the pdf file stuff' (length=30)
tags
object(SimpleXMLElement)[74]
public 'tag' => string 'PDFTag' (length=6)
timeSignature
object(SimpleXMLElement)[72]
public '@attributes' =>
array
'upper' => string '4' (length=1)
'lower' => string '4' (length=1)
key
object(SimpleXMLElement)[74]
string 'C' (length=1)
transposition
object(SimpleXMLElement)[72]
string 'PDF Trans' (length=9)
bpm
object(SimpleXMLElement)[74]
string '120' (length=3)
defaultAudio
object(SimpleXMLElement)[72]
string '57895336-6D03-41B4-954C-91DA3F512185-19726-00011178DFE613C5' (length=59)
我错过了什么?为什么它跳过了
@attributes
?SimpleXML是PHP中另一个奇怪的不一致性,它有点独特。它紧密集成在PHP核心中,并显示了PHP中其他类(包括本机类)无法显示的一些特性,例如,它代表了与PHP相关的一个特例
我可以整天喋喋不休地谈论SimpleXML的奇怪之处,但为了切中要害,@attributes
实际上向您展示了转换为数组的SimpleXMLElement
方法的结果。它实际上不是一个财产
我个人更喜欢使用与DOM相关的所有东西,因为虽然它更加臃肿和冗长,但我发现它并没有做我不期望的事情,SimpleXML就是这样做的。就我而言,这主要是用户错误/思维障碍,但它还伴随着稍微不充分的文档和一些不标准的地方——比如你在这里遇到的那个
为什么它会跳过@attributes
将foreach
与XML元素的simplexmlement
对象一起使用,只会在元素集合上循环,而不会循环任何属性(具体迭代哪些元素取决于访问对象的方式;它可以是所有子元素或具有特定本地名称的子元素)
如果要对元素的属性执行foreach
,请使用类似foreach($v->attributes()as$name=>$value)
的方法。此方法为属性返回一个simplexmlement
对象,可以对其进行迭代
值得一提的是,如果您只是想访问属性,则不需要
attributes()
;可以使用数组样式语法$v['attribute_name']
。迭代simplexmlement
是否只是在节点上循环?我认为您需要在每个节点上调用->attributes()
,以检查属性(包括根元素)。+1我忘记了SimpleXMLElement实现ArrayAccess
——据我所知,它完全没有文档化。但奇怪的是,当你将其转储时,它显示出好像有一个名为@attributes
的公共属性,这非常具有误导性,并且会引发类似于你在这里看到的问题。这正是我刚刚使用DOM并已使用过它的原因,它的迷惑性要小得多。@DaveRandom,simplexmlement
没有实现该接口,但它允许对属性进行数组样式的访问。可以使用数组样式访问的事实在页面上有简要的记录,因此它提供了一种只能通过实现接口而不能实现所述接口的语法?它提供的语法只能通过实现接口在userland代码中实现。核心/扩展代码是一个完全不同的beast。如果SimpleXMLElement
确实显式实现了ArrayAccess
,那就太好了。PHP内部人员已经多次讨论了(,)标量转换的其他神奇方法,并提供了各种补丁。还讨论了具有可比的接口()。这些事情最终可能会发生。
title
object(SimpleXMLElement)[74]
string 'The PDF File' (length=12)
summary
object(SimpleXMLElement)[72]
string 'Summary for the pdf file stuff' (length=30)
tags
object(SimpleXMLElement)[74]
public 'tag' => string 'PDFTag' (length=6)
timeSignature
object(SimpleXMLElement)[72]
public '@attributes' =>
array
'upper' => string '4' (length=1)
'lower' => string '4' (length=1)
key
object(SimpleXMLElement)[74]
string 'C' (length=1)
transposition
object(SimpleXMLElement)[72]
string 'PDF Trans' (length=9)
bpm
object(SimpleXMLElement)[74]
string '120' (length=3)
defaultAudio
object(SimpleXMLElement)[72]
string '57895336-6D03-41B4-954C-91DA3F512185-19726-00011178DFE613C5' (length=59)