Ema*_*sev 2 php iteration recursion multidimensional-array
假设您有以下数组:
$nodes = array(
"parent node",
"parent node",
array(
"child node",
"child node",
array(
"grand child node",
"grand child node")));
Run Code Online (Sandbox Code Playgroud)
您将如何将其转换为XML字符串,使其看起来像:
<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>
Run Code Online (Sandbox Code Playgroud)
一种方法是通过递归方法,如:
function traverse($nodes)
{
echo "<node>";
foreach($nodes as $node)
{
if(is_array($node))
{
traverse($node);
}
else
{
echo "<node>$node</node>";
}
}
echo "</node>";
}
traverse($nodes);
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种使用迭代的方法.
Gor*_*don 15
您可以使用迭代器迭代数组,然后生成所需的输出:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后像这样组装:
$iterator = new TranformArrayIterator(new RecursiveArrayIterator($nodes));
foreach($iterator as $val) {
echo $val;
}
Run Code Online (Sandbox Code Playgroud)
输出
<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>
Run Code Online (Sandbox Code Playgroud)
要$key在使用时消隐$key => $val,请将其添加到TraverseArrayIterator
public function key()
{
return '';
}
Run Code Online (Sandbox Code Playgroud)
由于您的目标似乎是生成XML,因此您还可以将XMLWriter作为协作者传递给Iterator.这允许更多地控制生成的XML,并确保输出是有效的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());
}
}
Run Code Online (Sandbox Code Playgroud)
然后你会像这样使用它:
$xmlWriter = new XmlWriter;
$xmlWriter->openUri('php://output');
$xmlWriter->setIndent(true);
$xmlWriter->setIndentString("\t");
$iterator = new TranformArrayIterator(
$xmlWriter,
new RecursiveArrayIterator($nodes)
);
Run Code Online (Sandbox Code Playgroud)
并且foreach'结束它将产生相同的输出然后(但添加XML序言)