views:

330

answers:

3

Say you have the following array:

$nodes = array(
    "parent node",
    "parent node",
    array(
        "child node",
        "child node",
        array(
            "grand child node",
            "grand child node")));

How would you go about transforming it to an XML string so that it looks like:

<node>
    <node>parent node</node>
    <node>parent node</node>
    <node>
        <node>child node</node>
        <node>child node</node>
        <node>
            <node>grand child node</node>
            <node>grand child node</node>
        </node>
    </node>
</node>

One way to do it would be through a recursive method like:

function traverse($nodes)
{
    echo "<node>";

    foreach($nodes as $node)
    {
        if(is_array($node))
        {
            traverse($node);
        }
        else
        {
            echo "<node>$node</node>";
        }
    }

    echo "</node>";
}

traverse($nodes);

I'm looking for an approach that uses iteration, though.

+1  A: 

To 'unroll' recursion this way, you need to use a stack. This article explains how in, VBscript unfortunately.

C. Ross
+3  A: 

See SplIterators

class TranformArrayIterator extends RecursiveIteratorIterator
{
    protected function indent()
    {
        echo str_repeat("\t", $this->getDepth());
        return $this;
    }
    public function beginIteration()
    {
        echo '<nodes>', PHP_EOL;
    }
    public function endIteration()
    {
        echo '</nodes>', PHP_EOL;
    }
    public function beginChildren()
    {
        $this->indent()->beginIteration();
    }
    public function endChildren()
    {
        $this->indent()->endIteration();
    }
    public function current()
    {
        return sprintf('%s<node>%s</node>%s',
                       str_repeat("\t", $this->getDepth() +1),
                       parent::current(),
                       PHP_EOL);
    }
}

and

$iterator = new TranformArrayIterator(
                new RecursiveArrayIterator($nodes));

foreach($iterator as $val) {
    echo $val;
}

outputs

<nodes>
        <node>parent node</node>
        <node>parent node</node>
        <nodes>
                <node>child node</node>
                <node>child node</node>
                <nodes>
                        <node>grand child node</node>
                        <node>grand child node</node>
                </nodes>
        </nodes>
</nodes>

To blank out $key when using $key => $val, add this to TraverseArrayIterator

public function key()
{ 
    return '';
}

Yes, it's recursive, but also as simple and clean as can be.

Gordon
+2  A: 
<?php

$nodes = array(
    "parent node",
    "parent node",
    array(
        "child node",
        "child node",
        array(
            "grand child node",
            "grand child node"
        )
    )
);

$s = '<node>';
$arr = $nodes;

while(count($arr) > 0)
{
    $n = array_shift($arr);
    if(is_array($n))
    {
        array_unshift($arr, null);
        $arr = array_merge($n, $arr);
        $s .= '<node>';
    }
    elseif(is_null($n))
        $s .= '</node>';
    else
        $s .= '<node>'.$n.'</node>';
}
$s .= '</node>';

echo $s;

?>
Benoit Vidis
Brilliant! I wonder if this is faster than the recursion-based approach.
Emanuil