【发布时间】:2023-03-15 21:03:01
【问题描述】:
谁能解释一下?
foreach ($xml as $product) {
为什么这样可以删除节点
unset ($product->itemspecific);
unset ($product->properties);
但这不会删除整个元素?
unset ($product);
}
编辑:
$result = $soap->GetFeedXML( array('accessKey' => $feed) );
//Write XML data to file
$bytes = file_put_contents($file,$result->GetFeedXMLResult);
$xml = new SimpleXMLElement($result->GetFeedXMLResult);
foreach ($xml as $product) {
//Remove fluous properties
unset ($product->ebay_itemspecific);
unset ($product->StoneProperties);
unset ($product->StoneValues);
unset ($product->UserCity);
unset ($product->eBayStartingPrice);
unset ($product->ebay_quantity);
unset ($product->ebay_variation);
unset ($product->Amazon_Metal_Type);
unset ($product->Amazon_Item_Type);
unset ($product->Ebay_Category_Number);
unset ($product->DropshipUser_Zip);
unset ($product->DropshipUser_Company);
unset ($product->PayPal_Email_Address);
//Convert different product sizes into options for the single product
$x = strpos($product->StockNo,"-");
if ($x > 0) {
$stock = substr($product->StockNo,0,$x);
$size = substr($product->StockNo,$x+1);
//Find the parent
$p = $xml->xpath("ProductFeedEntity[StockNo='$stock']");
$option = $p[0]->addChild("Options")->addChild("Size");
$option->addChild("Name","Size");
$option->addChild("Value", $size);
$option->addChild("CostPrice", $product->CostPrice);
echo "<i> Added size: " . $size . "</i><br>";
这就是问题所在。我试过使用 $index 和没有。我试过 unset($xml[$index]) 但这也不起作用。
//Remove the sized product
unset($product[$index]);
} else
echo $product->StockNo . " - " . $product->ItemTitle . "<br>";
$index++;
}
$xml->asXml($file);
XML
<ProductFeedEntity>
<StockNo>123</StockNo>
<MoreNodes>XXX</MoreNodes>
<MoreNodes>XXX</MoreNodes>
</ProductFeedEntity>
<ProductFeedEntity>
<StockNo>456</StockNo>
<MoreNodes>XXX</MoreNodes>
<MoreNodes>XXX</MoreNodes>
</ProductFeedEntity>
<ProductFeedEntity>
<StockNo>456-A</StockNo>
<MoreNodes>XXX</MoreNodes>
<MoreNodes>XXX</MoreNodes>
</ProductFeedEntity>
<ProductFeedEntity>
<StockNo>789</StockNo>
<MoreNodes>XXX</MoreNodes>
<MoreNodes>XXX</MoreNodes>
</ProductFeedEntity>
【问题讨论】:
-
您现在是否质疑为什么:
unset ($product);不起作用或如何解决? -
循环内
foreach()正在使用原始数组的副本。你想在这里实现什么目标? -
我知道它是按价值计算的,而不是按引用计算的……但这让我感到困惑,为什么要删除 $product 节点而不是整个 $product 本身。我希望能够在 foreach 中删除整个 $product。
-
你必须从原来的中删除 unset($xml[KEY]);
-
我在我的问题中添加了更多代码。