《PHP學習:php實現將數組轉換為XML的方法》要點:
本文介紹了PHP學習:php實現將數組轉換為XML的方法,希望對您有用。如果有疑問,可以聯系我們。
PHP應用本文實例講述了php實現將數組轉換為XML的辦法.分享給大家供大家參考.具體如下:
PHP應用1. php代碼如下:
PHP應用
<?php
class A2Xml {
private $version = '1.0';
private $encoding = 'UTF-8';
private $root = 'root';
private $xml = null;
function __construct() {
$this->xml = new XmlWriter();
}
function toXml($data, $eIsArray=FALSE) {
if(!$eIsArray) {
$this->xml->openMemory();
$this->xml->startDocument($this->version, $this->encoding);
$this->xml->startElement($this->root);
}
foreach($data as $key => $value){
if(is_array($value)){
$this->xml->startElement($key);
$this->toXml($value, TRUE);
$this->xml->endElement();
continue;
}
$this->xml->writeElement($key, $value);
}
if(!$eIsArray) {
$this->xml->endElement();
return $this->xml->outputMemory(true);
}
}
}
$res = array(
'hello' => '11212',
'world' => '232323',
'array' => array(
'test' => 'test',
'b' => array('c'=>'c', 'd'=>'d')
),
'a' => 'haha'
);
$xml = new A2Xml();
echo $xml->toXml($res);
PHP應用2. 運行效果如下圖所示:
PHP應用
PHP應用PS:這里再為大家提供幾款關于xml操作的在線工具供大家參考使用:
PHP應用希望本文所述對大家的php程序設計有所贊助.
歡迎參與《PHP學習:php實現將數組轉換為XML的方法》討論,分享您的想法,維易PHP學院為您提供專業教程。