1

Can anybody explain this?

foreach ($xml as $product) {

Why does this work to remove the nodes

   unset ($product->itemspecific);
   unset ($product->properties);

But this doesn't remove the entire element?

   unset ($product);
}

EDIT:

$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>";

THIS IS WHERE THE PROBLEM IS. I've tried with an $index and without. I've tried unset($xml[$index]) but that doesn't work either.

       //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>

Bodi
  • 271
  • 3
  • 10
  • Is you question now why this: `unset ($product);` doesn't work or how to fix it? – Rizier123 Mar 09 '15 at 19:52
  • 4
    inside the loop `foreach()` is using a copy of the original array. what are you trying to achieve here? –  Mar 09 '15 at 19:52
  • I get that it's by value and not by ref...but it confuses me why the $product nodes get removed but not the entire $product itself. I'd like to be able to remove the entire $product within the foreach. – Bodi Mar 09 '15 at 19:58
  • you have to remove from the original unset($xml[KEY]); –  Mar 09 '15 at 20:02
  • I added more code to my question. – Bodi Mar 09 '15 at 20:24
  • Yes, you're understanding correctly, but it still doesn't work. When I echo $index it returns ProductFeedEntity (which is the main element node itself) instead of an actual incremental value. – Bodi Mar 09 '15 at 20:33
  • I added the XML. I want to delete an entire ProductFeedEntity if StockNo has a "-" in it. – Bodi Mar 09 '15 at 20:35
  • This might be helpful: [Remove a child with a specific attribute, in SimpleXML for PHP](http://stackoverflow.com/questions/262351/remove-a-child-with-a-specific-attribute-in-simplexml-for-php) – showdev Mar 09 '15 at 20:44
  • Tried that thread already lol. Didn't work. – Bodi Mar 09 '15 at 20:47

1 Answers1

0

You are iterating an object (SimpleXMLElement) so you should use the right syntax for unsetting element in a SimpleXMLElement.

Try this:

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

It seems from your examples you are generating the key as you go with an $index++. foreach provides the capability to get the keys of the array in the loop so you should just try using that.

Scopey
  • 6,269
  • 1
  • 22
  • 34
  • I tried that...it deleted EVERY node because they are all indexed from the foreach using "ProductFeedEntity" – Bodi Mar 09 '15 at 20:45
  • And I'm baffled as to why the dozen unset() lines just after the foreach ALL work fine...it's just the main element itself that never gets removed. – Bodi Mar 09 '15 at 20:46
  • There are nodes in every ProductFeedEntity that I don't want. There are also duplicate ProductFeedEntity's (for my purposes anyway; they're not TRUE dupes). The first bunch of unset's all work perfectly at removing the nodes of data I don't want. – Bodi Mar 09 '15 at 20:54
  • Try changing your if statement to: `if($x !== false)` – Scopey Mar 09 '15 at 20:54
  • The if() statement works fine already. It successfully executes the code I want....except the removal of that entire ProductFeedEntity of course. – Bodi Mar 09 '15 at 20:55
  • Does anybody have any other suggestions? Even if it's not with this method; maybe I have to use a different XML parser? Or even work with it as plain text. – Bodi Mar 14 '15 at 02:09