note 105002 added to book.simplexml
| From: | james at esilogix dot com | Date: | Thu, 21 Jul 2011 14:55:29 +0000 |
| Subject: | note 105002 added to book.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181742@lists.php.net to get a copy of this message | ||
Here is a function I wrote that converts an associative array into an xml output. It also includes
tabs to ensure the xml output is easy to read.
/*
* James Earlywine - July 20th 2011
*
* Translates a jagged associative array
* to XML
*
* @param : $theArray - The jagged Associative Array
* @param : $tabCount - for persisting tab count across recursive function calls
*/
function assocToXML ($theArray, $tabCount=2) {
//echo "The Array: ";
//var_dump($theArray);
// variables for making the XML output easier to read
// with human eyes, with tabs delineating nested relationships, etc.
$tabCount++;
$tabSpace = "";
$extraTabSpace = "";
for ($i = 0; $i<$tabCount; $i++) {
$tabSpace .= "\t";
}
for ($i = 0; $i<$tabCount+1; $i++) {
$extraTabSpace .= "\t";
}
// parse the array for data and output xml
foreach($theArray as $tag => $val) {
if (!is_array($val)) {
$theXML .=
PHP_EOL.$tabSpace.'<'.$tag.'>'.htmlentities($val).'</'.$tag.'>';
} else {
$tabCount++;
$theXML .=
PHP_EOL.$extraTabSpace.'<'.$tag.'>'.assocToXML($val, $tabCount);
$theXML .= PHP_EOL.$extraTabSpace.'</'.$tag.'>';
}
}
return $theXML;
}
----
Server IP: 69.147.83.197
Probable Submitter: 99.73.189.154
----
Manual Page -- http://www.php.net/manual/en/book.simplexml.php
Edit -- https://master.php.net/note/edit/105002
Del: integrated -- https://master.php.net/note/delete/105002/integrated
Del: useless -- https://master.php.net/note/delete/105002/useless
Del: bad code -- https://master.php.net/note/delete/105002/bad+code
Del: spam -- https://master.php.net/note/delete/105002/spam
Del: non-english -- https://master.php.net/note/delete/105002/non-english
Del: in docs -- https://master.php.net/note/delete/105002/in+docs
Del: other reasons-- https://master.php.net/note/delete/105002
Reject -- https://master.php.net/note/reject/105002
Search -- https://master.php.net/manage/user-notes.php