#native_company# #native_desc#
#native_cta#

XML To Nested

By Dan Watt
on July 15, 2001

Version: 1.1

Type: Function

Category: Other

License: GNU General Public License

Description: Function that takes the values array (3rd parameter) from xml_parse_into_struct, which is a 1-D array, and parses it into a nested array for easier traversal.

/*
XMLST_To_NESTED : by Dan Watt ([email protected])
Full Name 	: XML Structure to nested array
Purpose   	: xml_parse_into_struct produces two 1-dimensional arrays: One for all of the tags (values), and one for "looking up" (index)
			these tags. Well, this function	takes the values array and parses it into a nested array for much faster traversal.
Notes       : Not 100% Tested. Preserves all of the original structure data. Not tested on large files, so I can't guarantee speed at all
Why			: I was not pleased in having to either: 1) manually traverse and splice the value array, or 2) Using the index array to find
			the position of a tag (which also added the trouble of determining the parents of a given element). This method makes the data
            a little more structured for my purposes, and I figured someone else might find it handy.
Version 1.01: Fixed minor count issue, $c-$i-2 to $c-$i-1. Was preventing close tags from being placed in the appropriate place.
*/
function XMLST_To_NESTED($xmlstruct)
{
    $ret=array();
    for ($i=0; $i<count($xmlstruct); $i++)
    {
		$ret[$i]=$xmlstruct[$i];
        if ($xmlstruct[$i]["type"]=="open")
        {
        	$found=FALSE;
            for ($c=$i+1; (($c<count($xmlstruct)) AND ($found!=TRUE)); $c++)
            	if (($xmlstruct[$c]["level"]==$xmlstruct[$i]["level"]) AND ($xmlstruct[$c]["tag"]==$xmlstruct[$i]["tag"]) AND ($xmlstruct[$c]["type"]=="close"))
                {
                	$found=TRUE;
    	            $ret[$i]["subtags"]=XMLST_To_NESTED(array_splice($xmlstruct,$i+1,$c-$i-1));
	            }
        }
    }
	return $ret;
}