So you want to convert this tree data structure:
<total>
<tag>content</tag>
<tag2 ref="333">
<code>somecode</code>
<code>morecode</code>
</tag2>
<tag3> more code </tag3>
</total>
Into some sort of flat array:
Array
(
[0] => "<tag>content</tag>"
[1] => "<tag2 ref="333"></tag2>"
[2] => "<code>somecode</code>"
[3] => "<code>morecode</code>
[4] => "<tag3> more code </tag3> "
)
would be tricky. This is a classic CS problem that doesn't have a lot of good answerers. The tree structure provides information on the relationships between entries that a flat array or list does not. Any attempt to flatten the tree into a list will loose that referential context.
You could either explode the string and then walk through it keeping track of parent elements or ignoring them (see tag2). If I had to do something with the xml I would drop it in a SimpleXMLElement, which would produce something like this:
SimpleXMLElement Object
(
[tag] => content
[tag2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[ref] => 333
)
[code] => Array
(
[0] => somecode
[1] => morecode
)
)
[tag3] => more code
)
With this I can walk it with foreach and find out the tag and it's contents. I can test to see if the contents are strings or child elements and if so walk them. A recursive function would make fairly short work of this problem. The biggest issue is how to represent the data once it flattens.
If you flatten it into the array example I provided earlier the parent and child tags loose any implied relationship to each other. If this isn't a problem, great. Write the recursive function and you are done. Here is some psudocode:
function walking($content)
$out is the array chunk that is returned
foreach $content as $tag->$data
if $value is an SimpleXMLElement
collapse $data[@attributes] into a string $attributes
append <$tag $attributes></$tag> to the end of $out
you may need to remove @attributes before recursing.
recurse into walking($data) and append the returned array to the end of $out
if $value is an Array
append <$tag></$tag> to the end of $out
recurse into walking($data) and append the returned array to the end of $out
if $value is a string
append <$tag>$value</$tag> to the end of $out
after looping through $content return $out.
However if you need to somehow keep those relationships intact you have a bit of a problem and will need to devise some sort of scheme for that