foreach循环停止在第一个xml元素上

时间:2013-05-04 19:29:30

标签: php xml

我有以下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的输出是“Unknown Binding”&amp; B0058O8V9U

1 个答案:

答案 0 :(得分: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'然后''替代版本'。