note 75623 deleted from ref.xml by danbrown
| From: | danbrown@php.net | Date: | Wed, 03 Mar 2010 18:55:06 +0000 |
| Subject: | note 75623 deleted from ref.xml by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166620@lists.php.net to get a copy of this message | ||
Note Submitter: ricardo at sismeiro dot com
----
<?php
/**
* correction of the previous code
*/
/**
* Converts XML into Array
*
* @param array $result
* @param object $root
* @param string $rootname
*/
function convert_xml2array(&$result,$root,$rootname='root'){
$n=count($root->children());
if ($n>0){
/**
* start of the correction
*/
if (!isset($result[$rootname]['@attributes'])){
$result[$rootname]['@attributes']=array();
foreach ($root->attributes() as $atr=>$value){
$result[$rootname]['@attributes'][$atr]=(string)$value;
}
}
/**
* end of the correction
*/
foreach ($root->children() as $child){
$name=$child->getName();
convert_xml2array($result[$rootname][],$child,$name);
}
} else {
$result[$rootname]= (array) $root;
if (!isset($result[$rootname]['@attributes'])){
$result[$rootname]['@attributes']=array();
}
}
}
/**
* Example how to use the function convert_xml2array
*/
/**
* Return Array from a xml string
*
* @param string $xml
* @return array
*/
function get_array_fromXML($xml){
$result=array();
$doc=simplexml_load_string($xml);
convert_xml2array($result,$doc);
return $result['root'];
}
?>