0

I'm trying to unset a simpleXML object without success. Good records matches but many syntaxes failed to unset(), so I beg explication here^^. The XML looks like :

`...<data number="313" user="001" order="313">
<column>
    <parameter name="idcompte" type="cdata"><![CDATA[126]]></parameter>
    <parameter name="nomachat" type="cdata"><![CDATA[terreau universel LR]]></parameter>
    <parameter name="date" type="cdata"><![CDATA[2005-08-07]]></parameter>
    <parameter name="qte" type="cdata"><![CDATA[0.01]]></parameter>
    <parameter name="puht" type="cdata"><![CDATA[46.3150]]></parameter>
    <parameter name="unit" type="cdata"><![CDATA[m3]]></parameter>
    <parameter name="desc" type="cdata"><![CDATA[C5 de 10 tiges]]></parameter>
    <parameter name="paye" type="string">1</parameter>
    <parameter name="idcat" type="cdata"><![CDATA[048]]></parameter>
    <parameter name="culture" type="cdata"><![CDATA[067]]></parameter>
</column>

and an array list all $conditions (0=>array(0=>'idcat, 1=>048), 1=>array(idfourn etc.)) to mix filters on the $object by unseting wrong records.

$good=0;
        echo count($obj);
        foreach ($obj as $data) {
            //$number=$data['number'];
            foreach ($data->column->parameter as $b) {
                foreach ($conditions as $c) {
                    if((string)$b['name']==$c[0] and (string)$b==$c[1]){ 
                        ++$good;
                        //echo '<i>l\'attribut $bname ('.$b['name'].'='.(string)$b.')</i> soit c0:'.$c[0].' doit etre = à c1:'.$c[1].'<br/>';
                    }
                    else{ unset($data); }
                }
            }
        }
        echo '<b>'.$good.'</b> results';
        echo count($obj);

Unfortunately count() rest the same. Thanks from France for any help.

Chico
  • 1
  • 2
  • see this : http://stackoverflow.com/questions/27482851/php-remove-element-from-multidimensional-array-by-key-using-foreach/ – Pouya Darabi Dec 20 '14 at 14:57
  • try `unset($data[0])` – michi Dec 20 '14 at 21:19
  • `unset($data);` unsets the variable `$data` (compare with the PHP manual: http://php.net/unset), it does not remove anything from the XML document. I've closed your question against an existing duplicate that shows how to remove XML elements with SimpleXML if you've got an xpath result. It works by the so called *SimpleXML-Self-Reference*. Another alternative is to switch to **DOMDocument**: [How to delete element with DOMDocument?](http://stackoverflow.com/q/15272726/367456) - you can do from simplexml via http://php.net/dom_import_simplexml – hakre Dec 21 '14 at 09:11
  • thanx you all, i'll try with DOM for testing but anyway my simpleXml(code) needs to be converted to use array_multisort before return, so i unset() just before. Many links to read^^ have a nice week. – Chico Dec 22 '14 at 10:01

1 Answers1

0

use xpath() to select and unset() to delete:

$xml = simplexml_load_string($x); // assume XML in $x
$nodes = $xml->xpath("/column/parameter[@name='idcat' or @name='unit']"); 
foreach ($nodes as $node) unset($node[0]);

see the xpath-statement in line 2:

  • [] mark a condition
  • @ refers to an attribute rather than a child-node
  • the selected nodes will be stored in array $nodes as SimpleXML elements
  • I didn't fully understand your $condition-array, so I just selected 2 attributes from your example

use unset($node[0]) to delete

see it working: https://eval.in/236946

michi
  • 6,565
  • 4
  • 33
  • 56
  • Thanks you all, I'll test at work and bring back results, have a nice family day ! – Chico Dec 21 '14 at 08:00
  • your link is dead, thankx anyway – Chico Dec 22 '14 at 09:45
  • @chico strange, did work on 1st click, "500 server error" when repeated. My example above is sufficient to make it work. Success? – michi Dec 22 '14 at 21:39
  • sorry 'Internal Server Error' on first clic for me. However your code was efficient but I change mine to avoid the loop, with an xpath request which is faster. Thanks a lot for your attention. – Chico Dec 23 '14 at 07:09