0

我想将 php 数组转换为 xml。

我有这个代码:

$output=array("result" => "this is the result");

print assocArrayToXML("example",$output);


function assocArrayToXML($root_element_name,$ar) {

     $xml = new SimpleXMLElement("<?xml version=\"1.0\"?><{$root_element_name}></{$root_element_name}>");

     $f = create_function('$f,$c,$a','
        foreach($a as $k=>$v) {
                   if(is_array($v)) {
            $ch=$c->addChild($k);
            $f($f,$ch,$v);
        } else {
            $c->addChild($k,$v);
        }
    }');
    $f($f,$xml,$ar);
    return $xml->asXML();
    }

我在这里找到了功能:

http://php.net/manual/es/book.simplexml.php

当我执行代码时,我得到了这个:

this is the result

为什么我看不到xml?是因为它格式不正确吗?或者也许 Mozilla Firefox 不写它?

4

0 回答 0