note 109950 added to function.simplexml-load-string
| From: | Alex | Date: | Tue, 04 Sep 2012 17:20:55 +0000 |
| Subject: | note 109950 added to function.simplexml-load-string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190497@lists.php.net to get a copy of this message | ||
Small changes in the function written by Bob, now all the attributes are saved
<?php
function unserialize_xml($input, $callback = null, $recurse = false)
/* bool/array unserialize_xml ( string $input [ , callback $callback ] )
* Unserializes an XML string, returning a multi-dimensional associative array, optionally runs a
callback on all non-array data
* Returns false on all failure
* Notes:
* Root XML tags are stripped
* Due to its recursive nature, unserialize_xml() will also support SimpleXMLElement objects and
arrays as input
* Uses simplexml_load_string() for XML parsing, see SimpleXML documentation for more info
*/
{
// Get input, loading an xml string with simplexml if its the top level of recursion
$data = ((!$recurse) && is_string($input))? simplexml_load_string($input): $input;
// Convert SimpleXMLElements to array
$attributes = array();
if ($data instanceof SimpleXMLElement){
foreach($data as $key=>$at)
{
if($at->attributes())
{
$count = (phpversion() >= '5.3.0') ? $at->count() :
count($at->children());
if($count == 0) $attributes[$key][] = (array)$at;
}
}
$data = (array) $data;
if($attributes) $data = array_merge($data,$attributes);
}
// Recurse into arrays
if (is_array($data)) foreach ($data as &$item) $item = unserialize_xml($item, $callback,
true);
// Run callback and return
return (!is_array($data) && is_callable($callback))? call_user_func($callback, $data):
$data;
}
?>
----
Server IP: 192.41.42.26
Probable Submitter: 82.117.236.134
----
Manual Page -- http://www.php.net/manual/en/function.simplexml-load-string.php
Edit -- https://master.php.net/note/edit/109950
Del: integrated -- https://master.php.net/note/delete/109950/integrated
Del: useless -- https://master.php.net/note/delete/109950/useless
Del: bad code -- https://master.php.net/note/delete/109950/bad+code
Del: spam -- https://master.php.net/note/delete/109950/spam
Del: non-english -- https://master.php.net/note/delete/109950/non-english
Del: in docs -- https://master.php.net/note/delete/109950/in+docs
Del: other reasons-- https://master.php.net/note/delete/109950
Reject -- https://master.php.net/note/reject/109950
Search -- https://master.php.net/manage/user-notes.php