I'm trying to figure out how to grab and print a specific IDValue
in the code below, but the number and order of ProductIdentifier
nodes varies per Product
, so I can't always get the correct value by using:
<?php print $productXML[0]->ProductIdentifier[2]->IDValue; ?>
Is there a way to do something like: "If ProductIDType
is 03, then print IDValue
of that child node"?
<Product>
<RecordReference>PublishingGroup</RecordReference>
<NotificationType>03</NotificationType>
<RecordSourceType>01</RecordSourceType>
<RecordSourceName>Publishing Group</RecordSourceName>
<ProductIdentifier>
<ProductIDType>01</ProductIDType>
<IDTypeName>ID</IDTypeName>
<IDValue>28831</IDValue>
</ProductIdentifier>
<ProductIdentifier>
<ProductIDType>02</ProductIDType>
<IDValue>0163668869</IDValue>
</ProductIdentifier>
<ProductIdentifier>
<ProductIDType>03</ProductIDType>
<IDValue>9180763668860</IDValue>
</ProductIdentifier>
<ProductIdentifier>
<ProductIDType>15</ProductIDType>
<IDValue>9180763668860</IDValue>
</ProductIdentifier>
UPDATE:
I tried using xpath, as suggested by the very helpful comments below, but it simply returned "Array" when trying to print via php. I tried several ways to fix it, taking note from other questions here on SO and on W3C docs and other places, but no luck.
Here's what I ultimately ended up doing:
<?php
foreach($productXML[0]->ProductIdentifier as $value) {
if($value->ProductIDType=='03')
{
print ($value->IDValue);
}
}
?>
Not the most elegant of solutions, but it outputs the proper value now.