note 111087 added to simplexmlelement.addchild
| From: | ivan dot kakurov at gmail dot com | Date: | Sun, 13 Jan 2013 04:09:16 +0000 |
| Subject: | note 111087 added to simplexmlelement.addchild | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-193007@lists.php.net to get a copy of this message | ||
Here's my solution for creating XML from Multidimensional Array.
<?php
//DATA
$xmlDAta = array(
array(
"name" => "nameVal",
"value" => "valVal",
"css" => "cssVal"
),
array(
"name" => "name1Val",
"value" => "val1Val",
"css" => "css1Val"
),
"tname" => array(
array(
"iTname" => "iTname",
"iTname2" => "iTname1",
"iTname2" => "iTname2",
"iTbname3" => array(
"iiTbname" => "tbName",
"iiTbname1" => "tbName1",
),
),
),
"tdata" => "otheerDAta"
);
/**
* Create XML using string or array
*
* @param mixed $data input data
* @param SimpleXMLElement $xml
* @param string $child name of first level child
*
* @return adding Xml formated data into SimpleXmlElement
*/
function data2XML(array $data, SimpleXMLElement $xml, $child = "items")
{
foreach($data as $key => $val) {
if(is_array($val)) {
if(is_numeric($key)) {
$node = $xml->addChild($child);
$nodes = $node->getName($child);
} else {
$node = $xml->addChild($key);
$nodes = $node->getName($key);
}
$node->addChild($nodes, self::data2Xml($val, $node));
} else {
$xml->addChild($key, $val);
}
}
}
//Use
$xml = new SimpleXMLElement("<root/>");
Util::data2XML($xmlDAta, $xml, "Items");
?>
----
Server IP: 69.147.83.199
Probable Submitter: 95.158.166.193
----
Manual Page -- http://php.net/manual/en/simplexmlelement.addchild.php
Edit -- https://master.php.net/note/edit/111087
Del: integrated -- https://master.php.net/note/delete/111087/integrated
Del: useless -- https://master.php.net/note/delete/111087/useless
Del: bad code -- https://master.php.net/note/delete/111087/bad+code
Del: spam -- https://master.php.net/note/delete/111087/spam
Del: non-english -- https://master.php.net/note/delete/111087/non-english
Del: in docs -- https://master.php.net/note/delete/111087/in+docs
Del: other reasons-- https://master.php.net/note/delete/111087
Reject -- https://master.php.net/note/reject/111087
Search -- https://master.php.net/manage/user-notes.php