Remove a child with a specific attribute, in SimpleXML for PHP

后端 未结 17 2375
星月不相逢
星月不相逢 2020-11-22 02:50

I have several identical elements with different attributes that I\'m accessing with SimpleXML:


    
    

        
17条回答
  •  -上瘾入骨i
    2020-11-22 02:58

    I was also strugling with this issue and the answer is way easier than those provided over here. you can just look for it using xpath and unset it it the following method:

    unset($XML->xpath("NODESNAME[@id='test']")[0]->{0});
    

    this code will look for a node named "NODESNAME" with the id attribute "test" and remove the first occurence.

    remember to save the xml using $XML->saveXML(...);

提交回复
热议问题