How to convert array to SimpleXML

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

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

30条回答
  •  生来不讨喜
    2020-11-21 07:30

    From PHP 5.4

    function array2xml($data, $root = null){
        $xml = new SimpleXMLElement($root ? '<' . $root . '/>' : '');
        array_walk_recursive($data, function($value, $key)use($xml){
            $xml->addChild($key, $value);
        });
        return $xml->asXML();
    }
    

提交回复
热议问题