我需要使用 xmlns 从 php 中的数组创建 xml
这是我的数组和所需的输出 XML
<?php
$aray= array(
'foo' => '12',
'boo' => '15',
'bar' => 'test value',
);
?>
期望的输出
<xml version="1.0" encoding="utf-16"?>
<opertaion xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<foo xmlns="http://www.w3schools.com">12</foo>
<boo xmlns="http://www.w3schools.com">15</boo>
<bar xmlns="http://www.w3schools.com">test value</bar>
</opertaion>
您可以使用 foreach() 遍历数组中的所有值并将数据附加到输出变量中。
<?php
$aray= array(
'foo' => '12',
'boo' => '15',
'bar' => 'test value',
);
$output = '<xml version="1.0" encoding="utf-16"><opertaion xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';
foreach ($array as $key => $value) {
$output .= '<'.$key.' xmlns="http://www.w3schools.com">'.$value.'</'.$key.'>';
}
$output .= '</opertaion>';
echo $output;
?>