note 77123 deleted from ref.simplexml by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 01:19:39 +0000 |
| Subject: | note 77123 deleted from ref.simplexml by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155516@lists.php.net to get a copy of this message | ||
Note Submitter: Leonid Kogan
----
if you want to export an array as xml you can use this script
<?php
$test=array("TEST"=>"nurso",
"none"=>null,
"a"=>"b",
array(
"c"=>"d",
array("d"=>"e")));
$xml=array_to_simplexml($test);
print->($xml);
print_r($xml->asXML());
function array_to_simplexml($array, $name="config" ,&$xml=null )
{
if(is_null($xml))
{
$xml = new SimpleXMLElement("<{$name}/>");
}
foreach($array as $key => $value)
{
if(is_array($value))
{
$xml->addChild($key);
array_to_simplexml($value, $name, $xml->$key);
}
else
{
$xml->addChild($key, $value);
}
}
return $xml;
}
?>