Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8523463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:24:59+00:00 2026-06-11T07:24:59+00:00

I’ve read a large number of articles and Stack Overflow questions about converting an

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:25:01+00:00Added an answer on June 11, 2026 at 7:25 am

    Pretty sure there are no flags available with say simpleXML or json_decode. I don’t think XML is intended to preserve that. The structure is not intended to convey that, can see how it leads to bad design. Specific to XML, you can get around this by use of sequence in an XSD. But your data looks more like a DOM.

    As a work around have you looked at parsing it with as a DOM Document and stepping through it? Not too much code to parse it yourself leveraging that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I know there's a lot of other questions out there that deal with this
I don't have much knowledge about the IPv6 protocol, so sorry if the question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.