I used this code below to get the array loops not to clobber each other.
function GetChildren($vals, &$i) {
$children = array();
while (++$i < sizeof($vals)) {
// compair type
switch ($vals[$i]['type']) {
case 'cdata':
$children[] = $vals[$i]['value'];
break;
case 'complete':
$children[] = array(
'tag' => $vals[$i]['tag'],
'attributes' => $vals[$i]['attributes'],
'value' => $vals[$i]['value']
);
break;
case 'open':
$children[] = array(
'tag' => $vals[$i]['tag'],
'attributes' => $vals[$i]['attributes'],
'value' => $vals[$i]['value'],
'children' => GetChildren($vals, $i)
);
break;
case 'close':
return $children;
}
}
}
function GetXMLTree($file) {
$data = implode('', file($file));
// by: [email]waldo@wh-e.com[/email] - trim space around tags not within
$data = eregi_replace(">"."[[:space:]]+"."<","><",$data);
// XML functions
$p = xml_parser_create();
// by: [email]anony@mous.com[/email] - meets XML 1.0 specification
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($p, $data, &$vals, &$index);
xml_parser_free($p);
$i = 0;
$tree = array();
$tree[] = array(
'tag' => $vals[$i]['tag'],
'attributes' => $vals[$i]['attributes'],
'value' => $vals[$i]['value'],
'children' => GetChildren($vals, $i)
);
return $tree;
}
#echo "<pre>";
$temp = array();
$temp = GetXMLTree($simple);
#print_r($temp);