<?php
/*
 * xml2array
 *
 * Copyright (c) 2004 Colin Viebrock <colin@viebrock.ca>
 *
 * Usage of the works is permitted provided that this
 * instrument is retained with the works, so that any entity
 * that uses the works is notified of this instrument.
 *
 * DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
 */


function xml2array($xml) {

    
$_data NULL;

    
$xp xml_parser_create();
    
xml_parser_set_option($xpXML_OPTION_CASE_FOLDINGfalse);
    
xml_parser_set_option($xpXML_OPTION_SKIP_WHITEtrue);

    
xml_parse_into_struct($xp,$xml,$vals,$index);
    
xml_parser_free($xp);

    
$temp $depth = array();
    
$dc = array();

    foreach(
$vals as $value) {

        
$p join('::'$depth);
    
        
$key $value['tag'];

        switch (
$value['type']) {
    
          case 
'open':
            
array_push($depth$key);
            
array_push($depth, (int)$dc[$p]++ );
            break;

          case 
'complete':
            
array_pop($depth);
            
array_push($depth$key);
            
$p join('::',$depth);
            
$temp[$p] = $value['value'];
            
array_pop($depth);
            
array_push($depth, (int)$dc[$p] );
            break;

          case 
'close':
            
array_pop($depth);
            
array_pop($depth);
            break;

        }

    }

    foreach (
$temp as $key=>$value) {

        
$levels explode('::',$key);
        
$num_levels count($levels);

        if (
$num_levels==1) {
            
$_data[$levels[0]] = $value;
        } else {
            
$pointer = &$_data;
            for (
$i=0$i<$num_levels$i++) {
                if ( !isset( 
$pointer[$levels[$i]] ) ) {
                    
$pointer[$levels[$i]] = array();
                }
                
$pointer = &$pointer[$levels[$i]];
            }
            
$pointer $value;
        }

    }

    return (
$_data);

}