I am having some difficulty wrapping my brain around this, since I am new to PHP.
由于我是PHP新手,所以我在这方面有点困难。
XML Document contains:
XML文档包含:
<containerDetails>
<ID> theid </id>
<OwnerDetails id = 23212>
<name> the name </name>
</OwnerDetails>
<OtherData>
asdfdsa
</OtherData>
</containerDetails>
And I can access The Owner Name via $current["OwnerDetails"]["Name"]
ok
我可以通过$current["Owner details "]["Name"]来访问所有者名称
However sometimes there are multiple ownerdetails:
然而,有时也有多种所有权细节:
<containerDetails>
<ID> theid </id>
<OwnerDetails id = 23212>
<name> the name </name>
</OwnerDetails>
<OwnerDetails id = 23233>
<name> other name </name>
</OwnerDetails>
<OtherData>
asdfdsa
</OtherData>
</containerDetails>
I can use a
我可以使用一个
foreach($current["OwnerDetails"] as $row)
echo $row["Name"]
and I see both names. But if there is only ONE OwnerDetails it won't display the name correctly.... How Do I reliably access this data even if I don't know if there will be one or multiple items?
我看到两个名字。但如果只有一个OwnerDetails它不会显示名字正确....我如何可靠地访问这些数据,即使我不知道是否会有一个或多个项目?
2 个解决方案
#1
3
The easiest way to deal with this might be something like the following, depending on your XML parsing library:
处理此问题的最简单方法可能是如下所示,具体取决于您的XML解析库:
// make sure you have an array containing OwnerDetails elements
$ownerDetails = isset($current["OwnerDetails"][0])
? $current["OwnerDetails"]
: array($current["OwnerDetails"]);
// now iterate over it
foreach ($ownerDetails as $row) {
echo $row["Name"];
}
However, SimpleXML can take care of this for you; SimpleXMLElement objects implement the Traversable interface, so you could use foreach
in either scenario.
然而,SimpleXML可以为您解决这个问题;SimpleXMLElement对象实现了可遍历接口,因此您可以在任何一种场景中使用foreach。
#2
1
I'm not sure what XML parsing function you're using, so it's hard to debug its precise behaviour.
我不确定您使用的是什么XML解析函数,因此很难调试它的精确行为。
If you use SimpleXML, then foreach ( $current->OwnerDetails as $row )
should work in both cases. Similarly, both $current->OwnerDetails->Name
and $current->OwnerDetails[0]->Name
will get you the Name
of the first child. SimpleXML overloads functionality to work smoothly in cases like this.
如果您使用SimpleXML,那么foreach ($current->OwnerDetails as $row)应该在这两种情况下都有效。同样,$current->OwnerDetails->名称和$current->OwnerDetails[0]->名称都将获得第一个孩子的名字。SimpleXML会重载功能,以便在这种情况下正常工作。
Note the ->
notation (property access) to refer to a child node. In SimpleXML, the ['string']
notation accesses attributes, e.g. $current->OwnerDetails['id']
.
请注意->符号(属性访问)来引用子节点。在SimpleXML中,['string']符号访问属性,例如$current->OwnerDetails['id']。
#1
3
The easiest way to deal with this might be something like the following, depending on your XML parsing library:
处理此问题的最简单方法可能是如下所示,具体取决于您的XML解析库:
// make sure you have an array containing OwnerDetails elements
$ownerDetails = isset($current["OwnerDetails"][0])
? $current["OwnerDetails"]
: array($current["OwnerDetails"]);
// now iterate over it
foreach ($ownerDetails as $row) {
echo $row["Name"];
}
However, SimpleXML can take care of this for you; SimpleXMLElement objects implement the Traversable interface, so you could use foreach
in either scenario.
然而,SimpleXML可以为您解决这个问题;SimpleXMLElement对象实现了可遍历接口,因此您可以在任何一种场景中使用foreach。
#2
1
I'm not sure what XML parsing function you're using, so it's hard to debug its precise behaviour.
我不确定您使用的是什么XML解析函数,因此很难调试它的精确行为。
If you use SimpleXML, then foreach ( $current->OwnerDetails as $row )
should work in both cases. Similarly, both $current->OwnerDetails->Name
and $current->OwnerDetails[0]->Name
will get you the Name
of the first child. SimpleXML overloads functionality to work smoothly in cases like this.
如果您使用SimpleXML,那么foreach ($current->OwnerDetails as $row)应该在这两种情况下都有效。同样,$current->OwnerDetails->名称和$current->OwnerDetails[0]->名称都将获得第一个孩子的名字。SimpleXML会重载功能,以便在这种情况下正常工作。
Note the ->
notation (property access) to refer to a child node. In SimpleXML, the ['string']
notation accesses attributes, e.g. $current->OwnerDetails['id']
.
请注意->符号(属性访问)来引用子节点。在SimpleXML中,['string']符号访问属性,例如$current->OwnerDetails['id']。