在PHP中实现多级"迭代器"

Ale*_*lex 5 php oop iterator class nested-loops

我正在尝试创建一个像这样的迭代器,用于注释列表:

// the iterator class, pretty much the same as the one from the php docs...
abstract class MyIterator implements Iterator{

  public $position = 0,
         $list;

  public function __construct($list) {
    $this->list = $list;
    $this->position = 0;
  }

  public function rewind() {
    $this->position = 0;
  }

  public function current() {
    return $this->list[$this->position];
  }

  public function key() {
    return $this->position;
  }

  public function next() {
    ++$this->position;
  }

  public function valid() {
    return isset($this->list[$this->position]);
  }
}
Run Code Online (Sandbox Code Playgroud)

评论迭代器:

class MyCommentIterator extends MyIterator{

  public function current(){
    return new Comment($this->list[$this->position]);
  }    
}
Run Code Online (Sandbox Code Playgroud)

这就是我使用它的方式:

$comments = GetComments(); // gets the comments from the db
if($comments): ?>

  <ol>
    <?php foreach(new MyCommentIterator($comments) as $comment): ?>
    <li>
      <p class="author"><?php echo $comment->author(); ?></p>

      <div class="content">
        <?php echo $comment->content(); ?>
      </div>

      <!-- check for child comments and display them -->

    </li>
    <?php endforeach; ?>
  </ol>

<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)

所以一切都很好,除了一件事:我无法弄明白如何处理嵌套的评论:(

$comments数组返回一个平面的注释列表,如:

[0] => object(
    'id' => 346,
    'parent' => 0,  // top level comment
    'author' => 'John',
    'content' => 'bla bla'         
  ),

[1] => object(
    'id' => 478,
    'parent' => 346,  // child comment of the comment with id =346
    'author' => 'John',
    'content' => 'bla bla'         
  )
...
Run Code Online (Sandbox Code Playgroud)

我需要以某种方式能够检查子评论(在多个级别)并在</li>他们的父评论之前插入它们...

有任何想法吗?

uml*_*cat 1

您使用的是平面数组,但实际上,该数组的项目是树或分层数据结构。

您基本上显示的是一个顺序列表。也许您应该首先构建一个树/分层数据结构,而不显示,然后显示树列表中的数据。

/* array */ function FlatArrayToTreeArray(/* array */ $MyFlatArray)
{
  ...
}

/* void */ function IterateTree(/* array */ $MyTreeArray)
{
  ...
}

/* void */ function Example() {
  $MyFlatArray = Array(
  0 => object(
      'id' => 346,
      'parent' => 0,  // top level comment
      'author' => 'John',
      'title' => 'Your restaurant food its too spicy',
      'content' => 'bla bla'         
    ),
  1 => object(
      'id' => 478,
      'parent' => 346,  // child comment of the comment with id =346
      'author' => 'Mike',
      'title' => 'Re: Your restaurant food its too spicy',
      'content' => 'bla bla'         
    ),  
  2 => object(
      'id' => 479,
      'parent' => 478,  // child comment of the comment with id =346
      'author' => 'John',
      'title' => 'Re: Your restaurant food its too spicy',
      'content' => 'bla bla'         
    ),  
  3 => object(
      'id' => 479,
      'parent' => 346,  // child comment of the comment with id =346
      'author' => 'Jane',
      'title' => 'Re: Your restaurant food its too spicy',
      'content' => 'bla bla'         
    )
  );

  $MyTreeArray = FlatArrayToTreeArray($myflatarray);

  IterateTree($MyTreeArray);
} // function Example()
Run Code Online (Sandbox Code Playgroud)

干杯。