我有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