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

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

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


    
    

        
17条回答
  •  攒了一身酷
    2020-11-22 02:52

    This work for me:

    $data = '
    
    
    
    
    ';
    
    $doc = new SimpleXMLElement($data);
    
    $segarr = $doc->seg;
    
    $count = count($segarr);
    
    $j = 0;
    
    for ($i = 0; $i < $count; $i++) {
    
        if ($segarr[$j]['id'] == 'A12') {
            unset($segarr[$j]);
            $j = $j - 1;
        }
        $j = $j + 1;
    }
    
    echo $doc->asXml();
    

提交回复
热议问题