note 108001 deleted from class.simplexmliterator by danbrown

From: Date: Thu, 10 May 2012 17:59:24 +0000
Subject: note 108001 deleted from class.simplexmliterator by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-187699@lists.php.net to get a copy of this message
Note Submitter: Gordon ---- 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; } }

« previous php.notes (#187699) next »