我怎样才能获得对象的价值@

时间:2017-10-07 08:40:43

标签: php xml simplexml

我有一个对象,我从亚马逊回应得到。就像这个对象从xml转换为simplexml_load_string

["@attributes"]=> array(2) {
    ["condition"]=> string(3) "new" 
    ["fulfillmentChannel"]=> string(6) "Amazon"
}

我想获得属性的价值。 我该怎么办?!! 谢谢!

当我转换xml时,属性显示为[@attributes] ..这是xml的一部分:

        <BuyBoxEligibleOffers>
            <OfferCount condition="new" fulfillmentChannel="Amazon">1</OfferCount>
            <OfferCount condition="new" fulfillmentChannel="Merchant">10</OfferCount>
        </BuyBoxEligibleOffers>

1 个答案:

答案 0 :(得分:0)

您可以使用attributesSimpleXMLElement方法。

例如:

$source = <<<SOURCE
<BuyBoxEligibleOffers>
    <OfferCount condition="new" fulfillmentChannel="Amazon">1</OfferCount>
    <OfferCount condition="new" fulfillmentChannel="Merchant">10</OfferCount>
</BuyBoxEligibleOffers>
SOURCE;

$elements = simplexml_load_string($source);

foreach ($elements as $element) {
    echo "condition: " . $element->attributes()->condition . ", fulfillmentChannel: " . $element->attributes()->fulfillmentChannel . "<br>";
}

将导致:

  

条件:new,fulfillmentChannel:亚马逊

     

条件:new,fulfillmentChannel:Merchant