我有xml文件。如何从XML中的foreach中删除?
<shop>
<categories>
<category id="132">kids</category>
<category id="62" parentId="133">women</category>
<category id="172" parentId="1">men</category>
</categories>
<materials>
<material id="1">one</material>
<material id="2">two</material>
<material id="3">soon</material>
</materials>
<offers>
<offer id="41850" available="true">
<price>3220</price>
<currencyId>EUR</currencyId>
<date>2015-02-05</date>
</offer>
<offer id="41850" available="true">
<price>3220</price>
<currencyId>EUR</currencyId>
<date>2015-02-05</date>
</offer>
<offer id="41850" available="true">
<price>3220</price>
<currencyId>EUR</currencyId>
<date>2015-02-05</date>
</offer>
<offer id="77777" available="true">
<price>3250</price>
<currencyId>EUR</currencyId>
<date>2015-02-05</date>
</offer>
<offer id="41340" available="true">
<price>3120</price>
<currencyId>EUR</currencyId>
<date>2015-02-05</date>
</offer>
我需要删除的类别和材料。之后,我需要删除所有不是唯一的提供,并将其写入新的XML文件。
我的问题。我无法删除不唯一的优惠。请帮帮我!谢谢。这是我的代码。
$url = 'test.xml';
$yml = simplexml_load_file($url);
unset($yml->shop->categories);
unset($yml->shop->materials);
$itemid = '0';
foreach ($yml->shop->offers->offer as $item){
$sravnit = $item['id'];
if("$sravnit" == "$itemid") {
echo "$sravnit eq $itemid delete<br>";
unset($yml->shop->offers->offer[$sravnit]);
continue;
else {echo "$sravnit not eq $itemid ";
$itemid = $sravnit;
echo "itemid to - $itemid <br>";
}
}
$yml->asXML('odd.xml');
只是建立一个ID数组在循环。如果该ID已经在数组中,则可以将其删除。 – rjdown
当我使用unset($ yml-> shop-> categories);它的工作很好,但是当我使用unset($ yml-> shop-> offers-> offer [$ sravnit]);没什么作用...( – user1837120