How to update/modify the XML in foreach loop ?
I have an XML like so:
<items>
<item>
<client>1</client>
<retailer>RET1</retailer>
<code>c553d8be7f3e88bda6a26b8e7eacd11a6001e0ba5f3c6e26ddbf4a08d9e277f3</code>
<id>1234567891011</id>
</item>
<item>
<client>1</client>
<retailer>RET1</retailer>
<code>9afb1b44a582d76159626d05aaf414983625530d83cbfa40a1359bf0afd9225b</code>
<id>1234567891011</id>
</item>
<item>
<client>1</client>
<retailer>RET1</retailer>
<code>90f5ff04e07b0701b6db3c6e44ed2f1636b64fb9eb78f9121dc684b8b8339741</code>
<id>1234567891011</id>
</item>
</items>
In my code I have a foreach loop:
foreach( $xml as $code )
{
// Remove each item, so to keep the only one inside, and each time move forward by removing all other items
// if ($xml->node->code != $code )
// remove it
}
I don't know how do I remove each time not equal node to the code value, any suggestion ?
try with domdocument and removechild to remove the nodes.
$client = $items->getElementsByTagName('client')->item(0);
$items->removeChild($client);
You can do it like below:
<?php
$xml = simplexml_load_string($string);//$string contains your xml
$item = $xml->item;
$count_node = count($item);
for($i=0;$i<$count_node;$i++)
{
echo $item[$i]->code;// access the tags like this and check your conditions
}
?>
Let me know if you persist any issue
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.