views:

3468

answers:

5

I was wondering if anyone knew how to convert an array to a Simplexml object in PHP.

Thanks

A: 

IF the array is associative and keyed correctly, it would probably be easier to turn it into xml first. Something like:

function array2xml {
   foreach($array_item as $element => $value) {
            if (is_array($value)) {
                $xml .= "<$element>".array2xml($value)."</$element>";
                }
            else if($value = '')  {
                $xml .= "<$element />";
               }
            else {
               $xml .= "<$element>.htmlentities($value)."</$element>";
               }   
       }
return $xml;
}

$simple_xml = simplexml_load_string(array2xml($assoc_array));

The other route would be to create your basic xml first, like

$simple_xml = simplexml_load_string("<array></array>");

and then for each part of your array, use something similar to my text creating loop and instead use the simplexml functions "addChild" for each node of the array.

I'll try that out later and update this post with both versions.

Anthony
That bit where I mentioned "<array></array>" made me realize that the string version needs something similar. Basically the array has to have one node on the very outside. Let me sleep on the whole thing, I'll have something that catches that initial error right off.
Anthony
+8  A: 

a short one:

<?php

$test_array = array (
  'bla' => 'blub',
  'foo' => 'bar',
  'another_array' => array (
    'stack' => 'overflow',
  ),
);
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($test_array, array ($xml, 'addChild'));
print $xml->asXML();

results in

<?xml version="1.0"?>
<root>
  <blub>bla</blub>
  <bar>foo</bar>
  <overflow>stack</overflow>
</root>

keys and values are swapped - you could fix that with array_flip() before the array_walk. array_walk_recursive requires PHP 5. you could use array_walk instead, but you won't get 'stack' => 'overflow' in the xml then.

ax
This won't work if $test_array has 'more_another_array' like 'another_array', because key 'another_array' is not converted. Hence you'll have multiple '<overflow>stack</overflow>'.
understack
A: 

You may use xmlrpc_encode to create a xml from array if a verbose xml is not a problem. www.php.net/xmlrpc_encode

be careful the xml created differs in case you use associative and/or numeric keys

<?php
// /params/param/value/struct/member
// there is a tag "member" for each element
// "member" contains a tag "name". its value is the associative key
$xml1 = xmlrpc_encode(array('a'=>'b','c'=>'d'));
$simplexml1 = simplexml_load_string($xml1);
print_r($xml1);
print_r($simplexml1);

// /params/param/value/array/data
// there is a tag "data" for each element
// "data" doesn't contain the tag "name"
$xml2 = xmlrpc_encode(array('a','b'));
$simplexml2 = simplexml_load_string($xml2);
print_r($xml2);
print_r($simplexml2);
?>
w35l3y
A: 

For create XML with nested tags from the php array you can use this simple function:

/** Start learn Python with new http://appengine-framework.googlecode.com */
function arr2xml($arr) {
    $xml = "";
    foreach ($arr as $k=>$v) {
        if (is_array($v)) {
            # load nested elements
            $v = arr2xml($v);
        }
        $xml .= "<{$k}>{$v}</{$k}>\n";
    }
    return $xml;
}

$test_array = array (
  'bla' => 'blub',
  'foo' => 'bar',
  'another_array' => array (
    'stack' => 'overflow',
  ),
);
$xml = arr2xml($test_array);
print_r($xml);

In result wou can see this message: blub bar overflow

Anton Danilchenko
A: 
<?php
function array_to_xml(array $arr, SimpleXMLElement $xml)
{
    foreach ($arr as $k => $v) {
        is_array($v)
            ? array_to_xml($v, $xml->addChild($k))
            : $xml->addChild($k, $v);
    }
    return $xml;
}

$test_array = array (
    'bla' => 'blub',
    'foo' => 'bar',
    'another_array' => array (
        'stack' => 'overflow',
    ),
);

echo array_to_xml($test_array, new SimpleXMLElement('<root/>'))->asXML();
onokazu