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.

4
  • why are you looking for iteration-based approach? is this a homework? Commented Feb 5, 2010 at 13:53
  • Your example already uses iteration in combination with recursion. Commented Feb 5, 2010 at 13:55
  • @SilentGhost I would prefer a clean iteration based approach over a recursion-based one as it wouldn't burden the namespace with another function name. I guess this might not seem as a good enough reason but still... Commented Feb 5, 2010 at 14:04
  • 1
    ... You want to avoid making a function and just iterate inline? You're spending a lot of time trying to avoid doing it the easier, better way... Commented Feb 5, 2010 at 14:08

2 Answers 2

15

You could use an Iterator to iterate over the array and then produce your desired output:

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 then assemble it like this:

$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 '';
}

Since your aim seems to be to produce XML, you could also pass an XMLWriter as a collaborator to the Iterator. This allows for more control over the generated XML and also makes sure the output is valid XML:

class TranformArrayIterator extends RecursiveIteratorIterator
{
    private $xmlWriter;

    public function __construct(
        XmlWriter $xmlWriter, 
        Traversable $iterator, 
        $mode = RecursiveIteratorIterator::LEAVES_ONLY , 
        $flags = 0)
    {
        $this->xmlWriter = $xmlWriter;
        parent::__construct($iterator, $mode, $flags);
    }

    public function beginIteration()
    {
        $this->xmlWriter->startDocument('1.0', 'utf-8');
        $this->beginChildren();
    }
    public function endIteration()
    {
        $this->xmlWriter->endDocument();
    }
    public function beginChildren()
    {
        $this->xmlWriter->startElement('nodes');
    }
    public function endChildren()
    {
        $this->xmlWriter->endElement();
    }
    public function current()
    {
        $this->xmlWriter->writeElement('node', parent::current());
    }
}

You'd then use it like this:

$xmlWriter = new XmlWriter;
$xmlWriter->openUri('php://output');
$xmlWriter->setIndent(true);
$xmlWriter->setIndentString("\t");
$iterator = new TranformArrayIterator(
    $xmlWriter,
    new RecursiveArrayIterator($nodes)
);

and foreach'ing over it will produce the same output then (but adding the XML prolog)

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

Comments

2
<?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;

?>

1 Comment

Brilliant! I wonder if this is faster than the recursion-based approach.

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.