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

Posted by TimTowdi on Stack Overflow See other posts from Stack Overflow or by TimTowdi
Published on 2008-11-04T16:24:50Z Indexed on 2010/03/13 2:47 UTC
Read the original article Hit count: 643

Filed under:
|
|
|

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

<data>
    <seg id="A1"/>
    <seg id="A5"/>
    <seg id="A12"/>
    <seg id="A29"/>
    <seg id="A30"/>
</data>

I need to remove a specific seg element, with an id of "A12", how can I do this? I've tried looping through the seg elements and unsetting the specific one, but this doesn't work, the elements remain.

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

© Stack Overflow or respective owner

Related posts about php

Related posts about simplexml