note 54106 added to function.xml-parse-into-struct
| From: | PhFatmadmacdotfr at osu1 dot php dot net | Date: | Thu, 23 Jun 2005 20:11:24 +0000 |
| Subject: | note 54106 added to function.xml-parse-into-struct | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90862@lists.php.net to get a copy of this message | ||
The code previously posted by noob at noobsrule dot com doesn't work when the same tag name is
used at different levels.
(but perhaps "$php_used_prefix" was intended for that ?)
For example:
<RatedShipment>
<TransportationCharges>
...
</TransportationCharges>
<RatedPackage>
<TransportationCharges>
...
</TransportationCharges>
</RatedPackage>
</RatedShipment>
<?php
$p = xml_parser_create();
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($p, $UPSxml, $values, $idx);
xml_parser_free($p);
// tracking used keys
$usedKeys = array();
$deepLevel = -1;
// start a php array string (evaluated later)
$forEvalPrefix = '$xml_array';
// loop throught the value array
foreach ($values as $key => $val) {
$tagName = $val['tag']; // pass the key tag into a more friendly looking variable
$level = $val['level']; // idem
if($val['type'] == 'open') {
$deepLevel++; // increase deep level
$forEvalPrefix .= '[\''. $tagName .'\']';
// begin used keys checks to allow multidimensionatity under the same tag
(isset($usedKeys[$level][$tagName])) ? $usedKeys[$level][$tagName]++ :
$usedKeys[$level][$tagName] = 0;
$forEvalPrefix .= '['. $usedKeys[$level][$tagName] .']';
}
if($val['type'] == 'complete') {
($level > $deepLevel) ? $deepLevel++ : ''; // increase $deepLevel only if current
level is bigger
$tagValue = addslashes($val['value'] ); // format the value for evaluation as a string
$forEvalSuffix = '[\''. $tagName .'\'] = \''. $tagValue
.'\';'; // create a string to append to the current prefix
$forEval = $forEvalPrefix . $forEvalSuffix; // (without "$php_used_prefix"...)
eval($forEval); // write the string to the array structure
}
if($val['type'] == 'close') {
unset($usedKeys[$deepLevel]); // Suppress tagname's keys useless
$deepLevel--;
$forEvalPrefix = substr($forEvalPrefix, 0, strrpos($forEvalPrefix, '[')); // cut off
the used keys node
$forEvalPrefix = substr($forEvalPrefix, 0, strrpos($forEvalPrefix, '[')); // cut off
the end level of the array string prefix
}
}
echo '<pre>';
print_r ($xml_array);
echo '</pre>';
?>
MADmac ;-)
----
Manual Page -- http://www.php.net/manual/en/function.xml-parse-into-struct.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54106
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54106&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54106&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54106&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54106&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54106&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54106&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54106&report=yes
Search -- http://master.php.net/manage/user-notes.php