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

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

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


    
    

        
17条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 02:50

    If you want to cut list of similar (not unique) child elements, for example items of RSS feed, you could use this code:

    for ( $i = 9999; $i > 10; $i--) {
        unset($xml->xpath('/rss/channel/item['. $i .']')[0]->{0});
    }
    

    It will cut tail of RSS to 10 elements. I tried to remove with

    for ( $i = 10; $i < 9999; $i ++ ) {
        unset($xml->xpath('/rss/channel/item[' . $i . ']')[0]->{0});
    }
    

    But it works somehow randomly and cuts only some of the elements.

提交回复
热议问题