// near the end of function xmlize() - change it to use result of xml_depth() by reference
|
$array[$tagname]["#"] =& xml_depth($vals, $i);
|
return $array;
|
|
// Then here is a more memory-efficient xml_depth():
|
/**
|
* @internal You don't need to do anything with this function, it's called by
|
* xmlize. It's a recursive function, calling itself as it goes deeper
|
* into the xml levels. If you make any improvements, please let me know.
|
* @param array $vals array of associative arrays, one per XML element. See 3rd parameter of PHP's xml_parse_into_struct().
|
* This function removes entries from $vals which were processed already, to save memory.
|
* @param int $i 0-based index in $vals, where processing starts
|
* @param int $valssize Max. index in $vals + 1. That will differ from count($vals)
|
* after the 1st call to xml_depth() for given $vals, since it removes entries from $vals as it goes.
|
* It's optional only when calling xml_depth() for the first time at the uppermost
|
* level (first time for given $vals). Basically, it can be set to size of $vals only before
|
* $vals was processed by any previous/upper calls to xml_depth().
|
* @return array of mixed-level structure, as used by Moodle restore
|
* @access private
|
*/
|
function &xml_depth(&$vals, &$i, $valssize=-1) {
|
if( $valssize<0 ) {
|
$valssize= count($vals);
|
}
|
$children = array();
|
|
if ( isset($vals[$i]['value']) )
|
{
|
array_push($children, $vals[$i]['value']);
|
}
|
|
while (++$i <$valssize ) {
|
|
switch ($vals[$i]['type']) {
|
case 'open':
|
if ( isset ( $vals[$i]['tag'] ) )
|
{
|
$tagname = $vals[$i]['tag'];
|
} else {
|
$tagname = '';
|
}
|
|
if ( isset ( $children[$tagname] ) )
|
{
|
$size = sizeof($children[$tagname]);
|
} else {
|
$size = 0;
|
}
|
|
if ( isset ( $vals[$i]['attributes'] ) ) {
|
$children[$tagname][$size]['@'] = $vals[$i]["attributes"];
|
|
}
|
|
$children[$tagname][$size]['#'] =& xml_depth($vals, $i, $valssize);
|
break;
|
|
case 'cdata':
|
array_push($children, $vals[$i]['value']);
|
break;
|
|
case 'complete':
|
$tagname = $vals[$i]['tag'];
|
|
if( isset ($children[$tagname]) )
|
{
|
$size = sizeof($children[$tagname]);
|
} else {
|
$size = 0;
|
}
|
|
if( isset ( $vals[$i]['value'] ) )
|
{
|
$children[$tagname][$size]["#"] = $vals[$i]['value'];
|
} else {
|
$children[$tagname][$size]["#"] = '';
|
}
|
|
if ( isset ($vals[$i]['attributes']) ) {
|
$children[$tagname][$size]['@']
|
= $vals[$i]['attributes'];
|
}
|
break;
|
|
case 'close':
|
unset( $vals[$i] );
|
return $children;
|
}
|
unset( $vals[$i] );
|
}
|
return $children;
|
}
|