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

  • Home
  • SEARCH
  • 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 310025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:47:44+00:00 2026-05-12T07:47:44+00:00

Is there an easy way to marshal a PHP associative array to and from

  • 0

Is there an easy way to marshal a PHP associative array to and from XML? For example, I have the following array:

$items = array("1", "2",
    array(
        "item3.1" => "3.1",
        "item3.2" => "3.2"
        "isawesome" => true
    )
);

How would I turn it into something similar to the following XML in as few lines as possible, then back again?

<items>
    <item>1</item>
    <item>2</item>
    <item>
        <item3_1>3.1</item3_1>
        <item3_2>3.2</item3_2>
        <isawesome>true</isawesome>
    </item>
</items>

I don’t really care if I have to change the array structure a bit or if the XML that comes out is different to the above example. I’ve been trying to work with PHP’s XMLReader and XMLWriter, but the documentation is so poor and the code I’ve produced as a consequence looks nothing like what I feel it should look like:

$xml = SomeXMLWriter::writeArrayToXml($items);
$array = SomeXMLWriter::writeXmlToArray($xml);

Does it really have to be any harder than that to get a basic, raw XML dump of a PHP array without writing my own custom class?

I try to avoid PEAR. In addition to the configuration headaches I’ve had with it, I’ve never stuck with any of the packages I’ve ever used from it.

  • 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-05-12T07:47:44+00:00Added an answer on May 12, 2026 at 7:47 am

    For those of you not using the PEAR packages, but you’ve got PHP5 installed. This worked for me:

    /**
     * Build A XML Data Set
     *
     * @param array $data Associative Array containing values to be parsed into an XML Data Set(s)
     * @param string $startElement Root Opening Tag, default fx_request
     * @param string $xml_version XML Version, default 1.0
     * @param string $xml_encoding XML Encoding, default UTF-8
     * @return string XML String containig values
     * @return mixed Boolean false on failure, string XML result on success
     */
    public function buildXMLData($data, $startElement = 'fx_request', $xml_version = '1.0', $xml_encoding = 'UTF-8') {
        if(!is_array($data)) {
            $err = 'Invalid variable type supplied, expected array not found on line '.__LINE__." in Class: ".__CLASS__." Method: ".__METHOD__;
            trigger_error($err);
            if($this->_debug) echo $err;
            return false; //return false error occurred
        }
        $xml = new XmlWriter();
        $xml->openMemory();
        $xml->startDocument($xml_version, $xml_encoding);
        $xml->startElement($startElement);
    
        /**
         * Write XML as per Associative Array
         * @param object $xml XMLWriter Object
         * @param array $data Associative Data Array
         */
         function write(XMLWriter $xml, $data) {
             foreach($data as $key => $value) {
                 if(is_array($value)) {
                     $xml->startElement($key);
                     write($xml, $value);
                     $xml->endElement();
                     continue;
                 }
                 $xml->writeElement($key, $value);
             }
         }
         write($xml, $data);
    
         $xml->endElement();//write end element
         //Return the XML results
         return $xml->outputMemory(true); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an easy way to iterate over an associative array of this structure
Is there an easy way to produce MSDN-style documentation from the Visual Studio XML
Is there an easy way in C# to create Ordinals for a number? For
Is there an easy way to avoid dealing with text encoding problems?
Is there an easy way to tell if a ruby script is already running
Is there an easy way to set the zoom level for a windows form
Is there an easy way to read an entire Access file (.mdb) into a
Is there an easy way of using the RegularExpressionValidator control while ignoring white space?
Is there an easy way to capitalize the first letter of a string and
Is there an easy way of finding out the host name of a machine

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.