note 69533 deleted from ref.xml by bjori
| From: | bjori@php.net | Date: | Mon, 09 Oct 2006 11:05:56 +0000 |
| Subject: | note 69533 deleted from ref.xml by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-118262@lists.php.net to get a copy of this message | ||
Note Submitter: dkflbk at nm dot ru
----
More than a year ago, i wrote xml2array function
(i'm the same "vladson at pc-labs dot info", i just don't have that e-mail any
more)
Now i think, why don't make things the other way around ? :)
<?php
function array_to_xml($array, $encoding=false, $level=0) {
$xml = '';
if ( $level == 0 ) {
$xml = '<?xml version="1.0"' . ($encoding ? ' encoding="' .
$encoding . '"' : '') . '?>'."\n";
}
$level++;
foreach($array as $key=>$value) {
if ( empty($value) ) {
if ( is_int($key) ) {
for($i=1; $i<$level; $i++) $xml .= "\t";
$xml .= '<int />' . "\n";
} elseif ( is_string($key) ) {
for($i=1; $i<$level; $i++) $xml .= "\t";
$xml .= '<'.$key.' />' . "\n";
}
} elseif ( is_string($value) || is_int($value) ) {
if ( is_int($key) ) {
for($i=1; $i<$level; $i++) $xml .= "\t";
$xml .= '<string>' . htmlspecialchars($value) . '</string>' .
"\n";
} elseif ( is_string($key) ) {
for($i=1; $i<$level; $i++) $xml .= "\t";
$xml .= '<'.$key.'>' . htmlspecialchars($value) .
'</'.$key.'>' . "\n";
}
} elseif ( is_array($value) ) {
if ( is_int($key) ) {
for($i=1; $i<$level; $i++) $xml .= "\t";
$xml .= '<array>' . "\n";
$xml .= array_to_xml($value, $encoding, $level);
for($i=1; $i<$level; $i++) $xml .= "\t";
$xml .= '</array>' . "\n";
} elseif ( is_string($key) ) {
for($i=1; $i<$level; $i++) $xml .= "\t";
$xml .= '<'.$key.'>' . "\n";
$xml .= array_to_xml($value, $encoding, $level);
for($i=1; $i<$level; $i++) $xml .= "\t";
$xml .= '</'.$key.'>' . "\n";
}
}
}
return $xml;
}
?>