note 63879 added to function.xml-parse-into-struct
| From: | matt at australiangamer dot com | Date: | Mon, 03 Apr 2006 02:58:37 +0000 |
| Subject: | note 63879 added to function.xml-parse-into-struct | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-107197@lists.php.net to get a copy of this message | ||
I liked VampBoy's code as it gave me structure pretty much as I wanted it. Just two notes,
though:
There is a bug in this code. subdivide() checks for pre-existing values, but not in the case of a
"complete" element.
In the following (appallingly bad) XML
<Names>
<Name>Matt</Name>
<Name>Stacy</Name>
<Names>
Only Stacy is added to Names in the array, wiping Matt in the process.
To fix this replace the following code
<?php
elseif ($dat[level] === $level && $dat[type] === "complete"){
$newarray[$dat[tag]]=$dat[value];
}
?>
with
<?php
elseif ($dat[level] === $level && $dat[type] === "complete"){
if (isset($newarray[$dat['tag']]) &&
is_array($newarray[$dat['tag']])){
$newarray[$dat['tag']][] = $dat['value'];
} elseif (isset($newarray[$dat['tag']]) &&
!is_array($newarray[$dat['tag']])){
$newarray[$dat['tag']] = array($newarray[$dat['tag']],
$dat['value']);
} else {
$newarray[$dat['tag']]=$dat['value'];
}
}?>
Oh, also, note that the $dat[level], etc, WILL generate warnings, and should be more correctly
written as $dat['level'].
The other thing I thought I should point out is that the array keys when created using
xml_parse_into_struct will be an UPPERCASE version of your existing element names. If case is
important, especially if you, like me, need mixedCase, do the following:
<?php
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0);
?>
----
Server IP: 203.16.234.91
Probable Submitter: 203.185.225.70
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.xml-parse-into-struct.php
Edit -- http://master.php.net/note/edit/63879
Del: integrated -- http://master.php.net/note/delete/63879/integrated
Del: useless -- http://master.php.net/note/delete/63879/useless
Del: bad code -- http://master.php.net/note/delete/63879/bad+code
Del: spam -- http://master.php.net/note/delete/63879/spam
Del: non-english -- http://master.php.net/note/delete/63879/non-english
Del: in docs -- http://master.php.net/note/delete/63879/in+docs
Del: other reasons-- http://master.php.net/note/delete/63879
Reject -- http://master.php.net/note/reject/63879
Search -- http://master.php.net/manage/user-notes.php