note 108001 added to class.simplexmliterator
| From: | Gordon | Date: | Wed, 21 Mar 2012 14:53:12 +0000 |
| Subject: | note 108001 added to class.simplexmliterator | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186757@lists.php.net to get a copy of this message | ||
A few small changes to ratfactor's code to allow for better key->value pairs (when no
children) and also allow for attributes.
class xmlit {
function xml2array($xml){
$sxi = new SimpleXmlIterator($xml);
return $this->sxiToArray($sxi);
}
/**
* @param SimpleXMLIterator $sxi
* @return array
*/
function sxiToArray($sxi){
$a = array();
$attribs = (array)$sxi->attributes();
if($attribs) {
$a = $attribs;
}
for( $sxi->rewind(); $sxi->valid(); $sxi->next() ) {
if(!array_key_exists($sxi->key(), $a)){
$a[$sxi->key()] = array();
}
if($sxi->hasChildren()){
$a[$sxi->key()][] = $this->sxiToArray($sxi->current());
}
else{
if($sxi->current()->attributes()) {
$a[$sxi->key()] = $this->sxiToArray($sxi->current());
$a[$sxi->key()]['value'] = strval($sxi->current());
} else {
$a[$sxi->key()] = strval($sxi->current());
}
}
}
return $a;
}
}
----
Server IP: 192.41.42.26
Probable Submitter: 98.191.178.254
----
Manual Page -- http://www.php.net/manual/en/class.simplexmliterator.php
Edit -- https://master.php.net/note/edit/108001
Del: integrated -- https://master.php.net/note/delete/108001/integrated
Del: useless -- https://master.php.net/note/delete/108001/useless
Del: bad code -- https://master.php.net/note/delete/108001/bad+code
Del: spam -- https://master.php.net/note/delete/108001/spam
Del: non-english -- https://master.php.net/note/delete/108001/non-english
Del: in docs -- https://master.php.net/note/delete/108001/in+docs
Del: other reasons-- https://master.php.net/note/delete/108001
Reject -- https://master.php.net/note/reject/108001
Search -- https://master.php.net/manage/user-notes.php