How to convert array to SimpleXML

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

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

30条回答
  •  情话喂你
    2020-11-21 07:09

    You may use xmlrpc_encode to create a xml from array if a verbose xml is not a problem. www.php.net/xmlrpc_encode

    be careful the xml created differs in case you use associative and/or numeric keys

    'b','c'=>'d'));
    $simplexml1 = simplexml_load_string($xml1);
    print_r($xml1);
    print_r($simplexml1);
    
    // /params/param/value/array/data
    // there is a tag "data" for each element
    // "data" doesn't contain the tag "name"
    $xml2 = xmlrpc_encode(array('a','b'));
    $simplexml2 = simplexml_load_string($xml2);
    print_r($xml2);
    print_r($simplexml2);
    ?>
    

提交回复
热议问题