note 105965 added to class.simplexmliterator
| From: | evgeniy at ierusalimov dot ru | Date: | Wed, 28 Sep 2011 15:35:31 +0000 |
| Subject: | note 105965 added to class.simplexmliterator | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183509@lists.php.net to get a copy of this message | ||
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
)
)
)
----
Server IP: 192.41.42.26
Probable Submitter: 93.81.240.35
----
Manual Page -- http://www.php.net/manual/en/class.simplexmliterator.php
Edit -- https://master.php.net/note/edit/105965
Del: integrated -- https://master.php.net/note/delete/105965/integrated
Del: useless -- https://master.php.net/note/delete/105965/useless
Del: bad code -- https://master.php.net/note/delete/105965/bad+code
Del: spam -- https://master.php.net/note/delete/105965/spam
Del: non-english -- https://master.php.net/note/delete/105965/non-english
Del: in docs -- https://master.php.net/note/delete/105965/in+docs
Del: other reasons-- https://master.php.net/note/delete/105965
Reject -- https://master.php.net/note/reject/105965
Search -- https://master.php.net/manage/user-notes.php