note 101016 deleted from book.simplexml by danbrown
| From: | danbrown@php.net | Date: | Sat, 20 Nov 2010 15:43:43 +0000 |
| Subject: | note 101016 deleted from book.simplexml by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174224@lists.php.net to get a copy of this message | ||
Note Submitter: neytrino1 at gmail dot com
----
function NXmlToArray( $mXml, $bInlineAttr=true, $bOneChildTypeInline=true ){
static $sAttrChildId = "id";
static $mOutputEncoding = "cp1251";
if( !is_object($mXml) ){
return $mXml;
}
$return = array();
// parse attributes
$arrAttr = array();
$arrTmpAttr = $mXml->attributes();
foreach( $arrTmpAttr as $sCode=>$mValue ){
$sValue = trim( mb_convert_encoding( (string)$mValue, $mOutputEncoding, "utf8" ) );
if( $bInlineAttr ){
$return[$sCode] = $sValue;
} else {
$return["@attr"][$sCode] = $sValue;
}
}
// parse child (value)
$arrChild = $mXml->children();
if( count($arrChild)==0 ){
$sValueTmp = trim( mb_convert_encoding( (string)$mXml, $mOutputEncoding, "utf8" ) );
$return["value"] = $sValueTmp;
} else {
foreach( $arrChild as $sCode=>$child ){
$value = NXmlToArray( $child, $bInlineAttr, $bOneChildTypeInline );
if( isset($value[$sAttrChildId]) ){
$return[$sCode][$value[$sAttrChildId]] = $value;
continue;
}
if( count($value)==1 && isset($value["value"]) ){
$value = $value["value"];
}
if( isset($return[$sCode]) ){
if( !is_array($return[$sCode]) || !isset($return[$sCode][0]) ){
$return[$sCode] = array( $return[$sCode] );
}
$return[$sCode][] = $value;
} else {
$return[$sCode] = $value;
}
}
if( $bOneChildTypeInline ){
if( count($return)==1 ){
$return = reset($return);
}
}
}
return $return;
}