note 49334 added to function.xml-parse-into-struct
| From: | noob at noobsrule dot com | Date: | Mon, 24 Jan 2005 18:59:16 +0000 |
| Subject: | note 49334 added to function.xml-parse-into-struct | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-83885@lists.php.net to get a copy of this message | ||
This is my first time contributing but I found all the examples either didn't work or were
beyond my comprehension. I also wanted to maintain the XML multi-dimensionality so I could do loops
based on keys I was expecting rather than numbers or some other whatnot. This script is probably
way inefficient, but should give you a multidimensional array with a depth that will match the XML
source. Great for situations when you aren't sure what you'll be getting back. Its also
only based on the values array of xml_parse_into_str() using the 3 tag types (open, complete, and
close) -- hopefully it will make more sense to other noobs.
Basically it just constructs a string ($php_prefix) that is tweaked if it hits an "open"
or "close" tag and evaluated if it hits a "complete" tag.
Thanks.
<?php
# initiate curl session...
$ch = curl_init( "http://yourtargethost" ) or
die ( "could not initialize curl" );
# set curl options...
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $request_str );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 0 );
curl_setopt( $ch, CURLOPT_REFERER, https://foobar.com"
);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
# execute curl commands...
$curl_result = curl_exec( $ch ) or die ( "could not execute the post" );
curl_close( $ch ); // close curl session
# pass the XML result into 2 arrays ( index and values )...
$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, $curl_result, $vals, $index);
xml_parser_free($p);
# create an empty used key tracker...
$used_keys = array();
# start a php array string (evaluated later)...
$php_prefix = "$"."my_array";
# loop through the values array...
foreach ($vals as $key => $val) {
if( $val['type'] == "open" ) {
$my_tag = $val['tag']; // pass the key tag into a more friendly looking variable...
$php_prefix .= "['".$my_tag."']"; # add a new level to the array
string...
# begin used key checks to allow multidimensionality under the same tag, adds a number to the
prefix if necessary...
if( ! array_key_exists( $my_tag, $used_keys ) ) {
$used_keys[$my_tag] = 0;
$php_prefix .= "[".$used_keys[$my_tag]."]";
}else {
$used_keys[$my_tag]++; // increment the key tracker to avoid overwriting existing key...
$php_prefix .= "[".$used_keys[$my_tag]."]";
}
} elseif( $val['type'] == "complete" ) {
$my_value = addslashes( $val['value'] ); # format the value for evaluation as a
string...
$php_suffix = "['".$val['tag']."'] =
\"".$my_value."\";"; # create a string with the tag and value to append to
the current prefix (just used here)...
$php_str = $php_prefix.$php_used_prefix.$php_suffix;
eval( $php_str ); # write the string to the array structure...
}
if( $val['type'] == "close" ) {
$php_prefix = substr( $php_prefix, 0, strrpos( $php_prefix, "[" ) ); # cut off the used
keys node...
$php_prefix = substr( $php_prefix, 0, strrpos( $php_prefix, "[" ) ); # cut off the end
level of the array string prefix...
}
}
echo "<pre>";
//print_r ($index);
//echo "<br><br><br>";
//print_r ($vals);
//echo "<br><br><br>";
print_r ($my_array);
echo "</pre>";
?>
----
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+49334
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49334&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49334&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49334&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49334&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49334&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49334&report=yes
Search -- http://master.php.net/manage/user-notes.php