您好,欢迎访问一九零五行业门户网

多维PHP数组怎么转换成xml格式的数据

多维php数组如何转换成xml格式的数据?
php数组是这样的:
array(4)
{
[auth]=> array(3)
{
[user]=> string(8) customer
[password]=> string(8) password
[context]=> string(1) 4
}
[owner]=> array(2)
{
[user]=> string(9) customer2
[context]=> string(1) 4
}
[language]=> string(2) en
[task]=> array(1)
{
[code]=> string(4) 0130
}
}
转换成xml格式后的数据格式是这样的:
customer
password
4
customer2
4
en
0130

php数组 xml
分享到: ?array(3)? { [user]=>?string(8)?customer? [passw...' data-pics=''>
------解决方案--------------------
$ar=array
(
auth=> array
(
user=> customer ,
password=> password ,
context=> 4
) ,
owner=> array
(
user=> customer2 ,
context=> 4
) ,
language=> en ,
task=> array
(
code=> 0130
)
);
$doc = new domdocument('1.0','utf-8');
// we want a nice output
$doc->formatoutput = true;
$root = $doc->createelement('request');
$root = $doc->appendchild($root);
foreach($ar as $title=>$title_v){
$title = $doc->createelement($title);
$title = $root->appendchild($title);
if(is_array($title_v)){
foreach($title_v as $k=>$v){
$k = $doc->createelement($k);
$k = $title->appendchild($k);
$text = $doc->createtextnode($v);
$text = $k->appendchild($text);
}
}else{
$text = $doc->createtextnode($title_v);
$text = $title->appendchild($text);
}
}
echo $doc->savexml();
------解决方案--------------------
$ar = array(
auth => array(
user => customer,
password => password,
context => 4,
),
owner => array(
user => customer2,
context => 4,
),
language => en,
task => array(
code => 0130,
),
);
$xml = simplexml_load_string('');
create($ar, $xml);
echo $xml->savexml();
function create($ar, $xml) {
foreach($ar as $k=>$v) {
if(is_array($v)) {
$x = $xml->addchild($k);
create($v, $x);
}else $xml->addchild($k, $v);
}
}
customer
password
4
customer2
4
en
0130

其它类似信息

推荐信息