I've read a large number of articles and Stack Overflow questions about converting an XML document or fragment into an array in PHP, but none that I've read so far have addressed my specific problem. Here's my dilemma, preceded by an example XML fragment:
<category>
<template>
<random>
<li>And a good</li>
<li>Pleasant</li>
<li>Good</li>
<li>Fantabulous</li>
</random>
<set name="TOD"><srai>time of day</srai></set> to you, <get name="name" />.
<random>
<li>How are you?</li>
<li>To what do I owe the pleasure of this visit?</li>
<li>May your Athlete's Foot be under control, and may the flying monkeys never come to take your dog!</li>
<li>I trust your <get name="TOD" /> is going well?</li>
<li>May your <get name="TOD" /> be as pleasant as possible.</li>
</random>
</template>
</category>
This is a REAL WORLD example of some of the XML my script will be dealing with. The sequence order of the XML tags needs to be preserved, as the parsed results need to be concatenated correctly in order to provide the proper result. So far, all of the methods for converting XML fragments into arrays have created arrays that no longer contain the correct order. As an example, here's a var dump of the above XML, once it's been converted into an array:
Template array Var Dump:
array(4) {
["random"]=>
array(2) {
[0]=>
array(1) {
["li"]=>
array(4) {
[0]=>
array(1) {
["text"]=>
string(10) "And a good"
}
[1]=>
array(1) {
["text"]=>
string(8) "Pleasant"
}
[2]=>
array(1) {
["text"]=>
string(4) "Good"
}
[3]=>
array(1) {
["text"]=>
string(11) "Fantabulous"
}
}
}
[1]=>
array(1) {
["li"]=>
array(5) {
[0]=>
array(1) {
["text"]=>
string(12) "How are you?"
}
[1]=>
array(1) {
["text"]=>
string(44) "To what do I owe the pleasure of this visit?"
}
[2]=>
array(1) {
["text"]=>
string(97) "May your Athlete's Foot be under control, and may the flying monkeys never come to take your dog!"
}
[3]=>
array(2) {
["text"]=>
array(2) {
[0]=>
string(12) "I trust your"
[1]=>
string(14) "is going well?"
}
["get"]=>
array(1) {
["@attributes"]=>
array(1) {
["name"]=>
string(3) "TOD"
}
}
}
[4]=>
array(2) {
["text"]=>
array(2) {
[0]=>
string(8) "May your"
[1]=>
string(27) "be as pleasant as possible."
}
["get"]=>
array(1) {
["@attributes"]=>
array(1) {
["name"]=>
string(3) "TOD"
}
}
}
}
}
}
["set"]=>
array(2) {
["@attributes"]=>
array(1) {
["name"]=>
string(3) "TOD"
}
["srai"]=>
array(1) {
["text"]=>
string(11) "time of day"
}
}
["text"]=>
array(2) {
[0]=>
string(7) "to you,"
[1]=>
string(1) "."
}
["get"]=>
array(1) {
["@attributes"]=>
array(1) {
["name"]=>
string(4) "name"
}
}
}
As can be seen, the array, when created, "lost" the sequence order of the XML fragment, and you can't iterate through the array in a linear manner to arrive at the proper response. This is the crux of my problem, and what I'm looking to "fix".
The method I used in this example was json_decode(json_encode($xml), true)
, but I've used other, more complex script functions, with pretty much the same results. So, just as I asked in the title of this post, how can I preserve the "tag order" when converting an XML fragment to an array in PHP?