note 109950 deleted from function.simplexml-load-string by danbrown
| From: | danbrown@php.net | Date: | Tue, 04 Sep 2012 18:30:04 +0000 |
| Subject: | note 109950 deleted from function.simplexml-load-string by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190498@lists.php.net to get a copy of this message | ||
Note Submitter: Alex
----
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;
}
?>