0

I am trying to convert a PHP array to valid XML. The problem I have is that the array has nested arrays with integer keys and I want them to belong to the same XML tag.

Using an array_to_xml function I found on stackoverflow helped me with the conversion, but converts the nested arrays with integer keys to invalid and unwanted xml keys like <0>< /0>.

Here the example code:

$array = array(test1 => 1, test2 => 2, test3 => array("a", "b", "c"));

$xml = new SimpleXMLElement('<parameters></parameters>');
array_to_xml($array, $xml);

$result = $xml -> asXML("test.xml");

which generates:

<?xml version="1.0"?>
  <parameters>
    <test1>1</test1>
    <test2>2</test2>
    <test3><0>a</0><1>b</1><2>c</2></test3>
</parameters>

However, I need it to look like:

<?xml version="1.0"?>
  <parameters>
    <test1>1</test1>
    <test2>2</test2>
    <test3>a</test3>
    <test3>b</test3>
    <test3>c</test3>
</parameters>

Is there an easy way to convert such a PHP array and generate the above XML structure?

P.S.: I use the following array_to_xml function:

function array_to_xml($array, &$xml) {

    foreach($array as $key => $value) {
        if(is_array($value)) {
            if(!is_numeric($key)){              
                $subnode = $xml->addChild("$key");
                array_to_xml($value, $subnode);
            } else {                
                array_to_xml($value, $xml);
            }
        } else {            
            $xml->addChild("$key","$value");
        }
    }
}
grueb
  • 123
  • 11
  • In the topic http://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml the @Hanmant answer have a condition to dealing with numeric tag name. – rogeriolino Dec 15 '16 at 18:55
  • unfortunately, this function only helps in the sense that it convert <0> to tags etc.. But in order to import this part again as an integer key array into PHP in a different program, I would need it to generate multiple -tags as in the preferred syntax, I posted. – grueb Dec 15 '16 at 19:07

1 Answers1

1

how about this:

// http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
function isAssoc(array $arr)
{
    if (array() === $arr) return false;
    return array_keys($arr) !== range(0, count($arr) - 1);
}

function array_to_xml($array, &$xml)
{

    foreach($array as $key => $value)
    {
        if(is_array($value))
        {
            if(isAssoc($value))
            {
                $Parent = $xml->addChild($key);
                foreach($value as $childKey => $childValue)
                {
                    $Parent->addChild($childKey, $childValue);
                }
            }
            else
            {
                foreach($value as $subvalue)
                 {
                    $xml->addChild("$key","$subvalue");
                 }
            }
        }
        else
        {
            $xml->addChild("$key","$value");
        }
    }
}

John...

John Mc Murray
  • 363
  • 5
  • 17
  • Thank you! The problem is that it does not generalize for this situation: $array = array(test1 => 1, test2 => 2, test3 => array("a", "b", "c"), test4 => array(first => "x", second => "y")); – grueb Dec 16 '16 at 00:07
  • What would you expect to see in that scenario? Have you tried my code? This is what I get running the above code with your new sample: 12abcxy – John Mc Murray Dec 16 '16 at 00:30
  • What I would need is: 12a‌​bcx‌y – grueb Dec 16 '16 at 00:51
  • Ok, in that case I've used a function from this link to determine if its and indexed array or associative and then to handle it correctly... See my updated answer. [Associative or indexed array](http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential) – John Mc Murray Dec 16 '16 at 05:39