Dùng array_walk_recursive() để chuyển mảng thành XML. Khóa mảng thành tên element, giá trị thành nội dung.
Ví dụ
<?php
$array = [
'name' => 'alex',
'empdept' => 'account',
'address' => ['city' => 'michigan']
];
$xml = new SimpleXMLElement(' ');
array_walk_recursive($array, [$xml, 'addChild']);
echo $xml->asXML();
?>
Kết quả
<?xml version="1.0"?>
<root>
<name>alex</name>
<empdept>account</empdept>
<city>michigan</city>
</root>
Lưu ý
Nếu lỗi Class 'SimpleXMLElement' not found, cài gói php-xml hoặc php-simplexml.