【问题标题】:Unset in foreach is working but not :sforeach 中的未设置有效但无效:s
【发布时间】: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>&nbsp;&nbsp;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]);
  • 我在我的问题中添加了更多代码。

标签: php unset


【解决方案1】:

您正在迭代一个对象 (SimpleXMLElement),因此您应该使用正确的语法来取消设置 SimpleXMLElement 中的元素。

试试这个:

foreach($xml as $key => $product) {
    // ...
    unset($xml->$key);
}

从您的示例看来,您正在使用$index++ 生成密钥。 foreach 提供了在循环中获取数组键的功能,因此您应该尝试使用它。

【讨论】:

  • 我试过了......它删除了每个节点,因为它们都是使用“ProductFeedEntity”从 foreach 中索引的
  • 我很困惑为什么在 foreach 之后的十几个 unset() 行都可以正常工作……它只是永远不会被删除的主要元素本身。
  • 每个 ProductFeedEntity 中都有我不想要的节点。还有重复的 ProductFeedEntity(无论如何,出于我的目的;它们不是真正的骗子)。第一批未设置的所有内容都可以完美地删除我不想要的数据节点。
  • 尝试将 if 语句更改为:if($x !== false)
  • if() 语句已经可以正常工作了。它成功地执行了我想要的代码......当然除了删除整个 ProductFeedEntity。
猜你喜欢
  • 1970-01-01
  • 2015-02-18
  • 2014-04-10
  • 2021-08-15
  • 2021-11-06
  • 1970-01-01
  • 2019-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多