-1

我有以下 XML 数据

 <Items>
    <Request>
    <IsValid>True</IsValid>
    <ItemLookupRequest>
    <Condition>All</Condition>
    <IdType>ISBN</IdType>
    <ItemId>0071762345</ItemId>
    <ResponseGroup>AlternateVersions</ResponseGroup>
    <SearchIndex>All</SearchIndex>
    <VariationPage>All</VariationPage>
    </ItemLookupRequest>
    </Request>
    <Item>
    <ASIN>0071762345</ASIN>
    <AlternateVersions>
    <AlternateVersion>
    <ASIN>B0058O8V9U</ASIN>
    <Title>
    Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (& Other Social Networks) [Paperback] Dave Kerpen Dave Kerpen
    </Title>
    <Binding>Unknown Binding</Binding>
    </AlternateVersion>
    <AlternateVersion>
    <ASIN>B00511ONPG</ASIN>
    <Title>
    Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (& Other Social Networks)
    </Title>
    <Binding>Kindle Edition</Binding>
    </AlternateVersion>
    <AlternateVersion>
    <ASIN>0071813721</ASIN>
    <Title>
    Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (& Other Social Networks)
    </Title>
    <Binding>Hardcover</Binding>
    </AlternateVersion>
    </AlternateVersions>
    </Item>
    <Item>
    <ASIN>B00511ONPG</ASIN>
    <AlternateVersions>
    <AlternateVersion>
    <ASIN>0071762345</ASIN>
    <Title>
    Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (And Other Social Networks)
    </Title>
    <Binding>Paperback</Binding>
    </AlternateVersion>
    <AlternateVersion>
    <ASIN>B0058O8V9U</ASIN>
    <Title>
    Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (& Other Social Networks) [Paperback] Dave Kerpen Dave Kerpen
    </Title>
    <Binding>Unknown Binding</Binding>
    </AlternateVersion>
    <AlternateVersion>
    <ASIN>0071813721</ASIN>
    <Title>
    Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (& Other Social Networks)
    </Title>
    <Binding>Hardcover</Binding>
    </AlternateVersion>
    </AlternateVersions>
    </Item>
    </Items>
    </ItemLookupResponse>

我正在使用代码搜索每个 Binding 元素,然后像这样使用它

foreach($xml->Items->Item->AlternateVersions->AlternateVersion->Binding as $BookBinding) { //loop through the xml data to find the correct ASIN for the kindle edition
    foreach ($xml->Items->Item->AlternateVersions->AlternateVersion->ASIN as $Kindlestring)
    {
        var_dump ($BookBinding);
        if (preg_match('/Kindle Edition/i',$BookBinding))
        {
            //do stuff
        }
    }
}

但仅获得 $Binding 和 $ASIN 的第一次迭代,而不是所有 4 个元素,var_dump 的输出是“未知绑定”和 B0058O8V9U

4

1 回答 1

0

这可能会帮助你

foreach($xml->Items->Item as $item){
    foreach($item->AlternateVersions->AlternateVersion as $alt_version){
        var_dump($alt_version->Binding);
        var_dump($alt_version->ASIN);
        ...
    }
}

首先遍历“item”,然后遍历“AlternateVersion”。

于 2013-05-04T19:45:22.343 回答