note 105965 deleted from class.simplexmliterator by danbrown

From: Date: Wed, 28 Sep 2011 15:48:32 +0000
Subject: note 105965 deleted from class.simplexmliterator by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-183510@lists.php.net to get a copy of this message
Note Submitter: evgeniy at ierusalimov dot ru ---- I'd like to introduce a new version of the function sxiToArray() (by ratfactor@gmail.com) which now produces more compact result (does not produce single-key arrays): function sxiToArray( $sxi ) { $a = array(); for( $sxi->rewind(); $sxi->valid(); $sxi->next() ) { if( $sxi->hasChildren() ) { if( isset($a[$sxi->key()]) ) { if( !isset($a[$sxi->key()][0]) ) $a[$sxi->key()] = array($a[$sxi->key()]); $a[$sxi->key()][] = sxiToArray($sxi->current()); } else $a[$sxi->key()] = sxiToArray($sxi->current()); } else { if( isset($a[$sxi->key()]) ) { if( !is_array($a[$sxi->key()]) ) $a[$sxi->key()] = array($a[$sxi->key()]); $a[$sxi->key()][] = strval($sxi->current()); } else $a[$sxi->key()] = strval($sxi->current()); } } return $a; } XML: <cats> <cat> <name>Jack</name> <age>2</age> <color>grey</color> <color>white</color> </cat> <cat> <name>Maxwell</name> <age>12</age> <color>orange</color> <color>black</color> </cat> <cat> <name>Benvolio</name> <age>2</age> <color>black</color> </cat> </cats> Result: Array ( [cat] => Array ( [0] => Array ( [name] => Jack [age] => 2 [color] => Array ( [0] => grey [1] => white ) ) [1] => Array ( [name] => Maxwell [age] => 12 [color] => Array ( [0] => orange [1] => black ) ) [2] => Array ( [name] => Benvolio [age] => 2 [color] => black ) ) )

« previous php.notes (#183510) next »