How can I set text value of SimpleXmlElement without using its parent?

前端 未结 2 1150
轻奢々
轻奢々 2020-11-27 23:35

I want to set text of some node found by xpath()


  
    text
          


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 23:53

    You can do with a SimpleXMLElement self-reference:

    $firstC->{0} = "Victory!!"; // hackity, hack, hack!
    //  -or-
    $firstC[0]   = "Victory!!";
    

    found after looking at

    var_dump((array) reset($xml->xpath("(//c)[3]")))
    

    This also works with unset operations as outlined in an answer to:

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

提交回复
热议问题