I was basically having a little problem with my test function which I thought I'd make to make things easier on myself.
function parse_xml_to_neat_array( $xml_data )
{
// Internal stack for keeping track of the depth of the array
$stack = array();
// Return array build
$return_array = array();
$p = xml_parser_create();
xml_parse_into_struct( $p, $xml_data, $vals, $index );
xml_parser_free( $p );
$array_ptr = & $return_array;
// print_r( $vals );
foreach( $vals as $data_array )
{
// Check for an opening data key
if( $data_array['type']=="open" )
{
// Put the current pointer on the stack and make a new array with a pointer to it
array_push( $stack, $array_ptr );
$array_ptr[ $data_array['tag'] ] = array();
$array_ptr = & $array_ptr[ $data_array['tag'] ];
}
// Check for a closing data key
elseif( $data_array['type']=="close" )
{
// Get the pointer off of the stack before this one
$array_ptr = & array_pop( $stack );
}
// Check for a complete data
elseif( $data_array['type']=="complete" )
{
// For complete data just add the tag and the value
$array_ptr[ $data_array['tag'] ] = $data_array['value'];
}
}
return( $return_array );
}
It's supposed to output the XML data in a neat little array with the proper keys values, and data. It's supposed to be a really neat (as in clean and arranged) multidimensional arrays with the string keys from the XML. Problem is, the references are really giving me a hard time.
The output was something like this from the XML:
Vals array
Array
(
[0] => Array
(
[tag] => PARA
[type] => open
[level] => 1
)
[1] => Array
(
[tag] => NOTE
[type] => complete
[level] => 2
[value] => simple note
)
[2] => Array
(
[tag] => PARA
[type] => close
[level] => 1
)
)
from an XML sample of this:
<para><note>simple note</note></para>
But of course that was a much more simple example. The actually XML file is much more in depth. But the point is you get how the structure is mostly.
Explaination:
$stack = pseudo-stack to hold the array reference points
$return_array = base array to add all of the XML elements to (also coincidentally the return array)
$array_ptr = reference to the current array level
Okay, so I designed the function to read it like this:
(1) On an opening tag: push the current reference on the $stack array then create a new array in the same level as the current stack reference and make the reference variable pointing to the current array level point to the new array level inside the current.
(2) On a closing tag: pop the previous reference point off of the stack (from the previous array leveL)
(3) On a complete (or data) tag: just add a new value with a key into the current array level.
In theory it sounds great to me. I think the problem may be with references that they just can't do what I want.
What do you think? Did I confuse ya?
I appreciate your help. Thank you.