Heads up! This post was written 15 years ago. Some information might be outdated or may have changed since then.
class xmlGen {
var $array = array();
var $xml = '';
private function strct($array) {
foreach ($array as $k => $v) {
$tag = $k;
if(is_int($tag)) $tag = 'a_'.$tag;
$this->xml .= "<$tag>";
if (is_array($v)) {
self::strct($v);
} else {
$this->xml .= $v;
}
$this->xml .= "$tag>";
}
}
public function gen($array, $root = 'root') {
self::strct($array);
$header = "<$root>";
$footer = "$root>";
echo $header;
echo $this->xml;
echo $footer;
}
} може да се използва по следния начин $sample = array(
'zadacha' => array(
'1' => 'probna',
'2' => 'product_compile',
),
'test' => date('d.m.Y'),
);
include ('array2xml.php');
$xml = new xmlGen();
header ("content-type: text/xml");
echo $xml->gen($sample); генерираният файл от примера е
probna
product_compile
31.10.2010