note 86428 added to function.simplexml-load-string
| From: | javalc6 at gmail dot com | Date: | Sat, 18 Oct 2008 13:25:57 +0000 |
| Subject: | note 86428 added to function.simplexml-load-string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-145467@lists.php.net to get a copy of this message | ||
I wanted to convert an array containing strings and other arrays of the same type into a simplexml
object.
Here is the code of the function array2xml that I've developed to perform this conversion.
Please note that this code is simple without any checks.
function array2xml($array, $tag) {
function ia2xml($array) {
$xml="";
foreach ($array as $key=>$value) {
if (is_array($value)) {
$xml.="<$key>".ia2xml($value)."</$key>";
} else {
$xml.="<$key>".$value."</$key>";
}
}
return $xml;
}
return simplexml_load_string("<$tag>".ia2xml($array)."</$tag>");
}
$test['type']='lunch';
$test['time']='12:30';
$test['menu']=array('entree'=>'salad',
'maincourse'=>'steak');
echo array2xml($test,"meal")->asXML();
----
Server IP: 80.241.173.254
Probable Submitter: 87.3.189.137
----
Manual Page -- http://www.php.net/manual/en/function.simplexml-load-string.php
Edit -- https://master.php.net/note/edit/86428
Del: integrated -- https://master.php.net/note/delete/86428/integrated
Del: useless -- https://master.php.net/note/delete/86428/useless
Del: bad code -- https://master.php.net/note/delete/86428/bad+code
Del: spam -- https://master.php.net/note/delete/86428/spam
Del: non-english -- https://master.php.net/note/delete/86428/non-english
Del: in docs -- https://master.php.net/note/delete/86428/in+docs
Del: other reasons-- https://master.php.net/note/delete/86428
Reject -- https://master.php.net/note/reject/86428
Search -- https://master.php.net/manage/user-notes.php