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

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

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


    
    

        
17条回答
  •  遥遥无期
    2020-11-22 02:59

    Your initial approach was right, but you forgot one little thing about foreach. It doesn't work on the original array/object, but creates a copy of each element as it iterates, so you did unset the copy. Use reference like this:

    foreach($doc->seg as &$seg) 
    {
        if($seg['id'] == 'A12')
        {
            unset($seg);
        }
    }
    

提交回复
热议问题