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");
        }
    }
}
2
  • In the topic stackoverflow.com/questions/1397036/… the @Hanmant answer have a condition to dealing with numeric tag name. Commented Dec 15, 2016 at 18:55
  • unfortunately, this function only helps in the sense that it convert <0> to <item0> 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 <test3>-tags as in the preferred syntax, I posted. Commented Dec 15, 2016 at 19:07

1 Answer 1

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...

Sign up to request clarification or add additional context in comments.

4 Comments

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"));
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: <parameters><test1>1</test1><test2>2</test2><test3>a</test3><test3>b</test3><test3>c</test3><test4>x</test4><test4>y</test4></parameters>
What I would need is: <parameters><test1>1</test1><test2>2</test2><test3>a</test3>‌​<test3>b</test3><tes‌​t3>c</test3><test4><first>x‌</first>​<second>y</second></test4></parameters>
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.