How to convert array to SimpleXML

前端 未结 30 2625
遇见更好的自我
遇见更好的自我 2020-11-21 06:52

How can I convert an array to a SimpleXML object in PHP?

30条回答
  •  爱一瞬间的悲伤
    2020-11-21 07:23

    With FluidXML you can generate, starting from a PHP Array, an XML for SimpleXML with... just two lines of code.

    $fluidxml  = fluidxml($array);
    $simplexml = simplexml_import_dom($fluidxml->dom());
    

    An example array could be

    $array = [ 'doc' => [
                  'fruit' => 'orange',
                  'cake'  => [
                       '@id' => '123', 
                       '@'   => 'tiramisu' ],
                  [ 'pasta' => 'matriciana' ],
                  [ 'pasta' => 'boscaiola'  ]
    ] ];
    

    https://github.com/servo-php/fluidxml

提交回复
热议问题