는 다음과 같은 배열을 말해봐다차원 배열을 반복
function traverse($nodes)
{
echo "<node>";
foreach($nodes as $node)
{
if(is_array($node))
{
traverse($node);
}
else
{
echo "<node>$node</node>";
}
}
echo "</node>";
}
traverse($nodes);
나는 반복을 사용하는 접근법을 찾고 있는데, 그래도. 이런 식으로 조립 한 후
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);
}
}
과 :
왜 반복 기반 방식을 찾고 있습니까? 숙제인가요? – SilentGhost
왜? 왜 그럴거야? –
예제에서는 이미 반복을 반복과 함께 사용합니다. – meagar