Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
实施“多层次”;迭代器;在PHP中_Php_Oop_Class_Iterator_Nested Loops - Fatal编程技术网

实施“多层次”;迭代器;在PHP中

实施“多层次”;迭代器;在PHP中,php,oop,class,iterator,nested-loops,Php,Oop,Class,Iterator,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 = $

我正在尝试创建一个迭代器,例如,用于注释列表:

// 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]);
  }
}
注释迭代器:

class MyCommentIterator extends MyIterator{

  public function current(){
    return new Comment($this->list[$this->position]);
  }    
}
我就是这样使用它的:

$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; ?>
我需要以某种方式检查子注释(在多个级别上),并将它们插入到父注释的
之前


有什么想法吗?

递归是你的朋友

displaycomment(comment):
    $html .= "<ol>" . comment->html;
    foreach comment->child:
        $html .= "<li>" . displaycomment(child) . "</li>";
    $html .= "</ol>";
    return $html;
displaycomment(注释):
$html.=“”。评论->html;
foreach注释->子项:
$html.=“
  • ”。显示注释(子项)。“
  • ”; $html.=”; 返回$html;

    本文中出现的所有代码都是伪代码。与真实代码的任何相似之处,无论是工作的还是损坏的,都纯粹是巧合。

    您可能需要研究一下。如果您使用该接口的方法扩展迭代器,那么您就能够使用的实例按顺序对注释进行迭代

    但是,由于您的输出是一个平面列表,因此您需要自行处理级别的逻辑,例如,向上按深度插入
    ,向下按深度插入


    使用标志来控制遍历子对象的顺序。

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

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

    /* 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()
    
    干杯

    /* 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()