By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

turn xml into 2 dimensional array

P: n/a
Well, www.php.net seems utterly crashed. Does anyone know how to get
an XML stream and turn it into a 2 dimensional array?
Jul 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
With total disregard for any kind of safety measures
lk******@geocities.com (lawrence) leapt forth and uttered:
Well, www.php.net seems utterly crashed. Does anyone know how to
get an XML stream and turn it into a 2 dimensional array?


Here's a function I use to do that:

function fetch_xml($xml){

if(is_file($xml)){
$xml_data = file_get_contents($xml);
}
else{
$xml_data = $xml;
}
$parser = xml_parser_create();
@xml_parse_into_struct($parser, $xml_data, $assoc_arr, $idx_arr);
@xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
@xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
$root_tag = $assoc_arr[0]['tag'];
$base_tag = strtolower($assoc_arr[1]['tag']);
$i = 0;
foreach($assoc_arr as $key => $element){
if($element['tag'] != $root_tag){
if(!preg_match('/^\s+$/', $element['value'])){
$tag = strtolower($element['tag']);
$items[$i][$tag] = $element['value'];
if($tag == $base_tag){
$i++;
}
}
elseif(isset($element['attributes'])){
$items[$i]['id'] = $element['attributes']['ID'];
}
}
}

return $items;
}

--
There is no signature.....
Jul 16 '05 #2

P: n/a
Phil Roberts <ph*****@HOLYflatnetSHIT.net> wrote in message news:<Xn*************************@216.196.97.132>. ..
With total disregard for any kind of safety measures
lk******@geocities.com (lawrence) leapt forth and uttered:
Well, www.php.net seems utterly crashed. Does anyone know how to
get an XML stream and turn it into a 2 dimensional array?


Here's a function I use to do that:

function fetch_xml($xml){

if(is_file($xml)){
$xml_data = file_get_contents($xml);
}
else{
$xml_data = $xml;
}
$parser = xml_parser_create();
@xml_parse_into_struct($parser, $xml_data, $assoc_arr, $idx_arr);
@xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
@xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
$root_tag = $assoc_arr[0]['tag'];
$base_tag = strtolower($assoc_arr[1]['tag']);
$i = 0;
foreach($assoc_arr as $key => $element){
if($element['tag'] != $root_tag){
if(!preg_match('/^\s+$/', $element['value'])){
$tag = strtolower($element['tag']);
$items[$i][$tag] = $element['value'];
if($tag == $base_tag){
$i++;
}
}
elseif(isset($element['attributes'])){
$items[$i]['id'] = $element['attributes']['ID'];
}
}
}

return $items;
}


Perfect. Thanks.
Jul 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.